Replace tearDown with addCleanup in unit tests

Bug #1480119 reported by Dave Chen
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Identity (keystone)
Fix Released
Low
Dave Chen

Bug Description

tearDown should be replace by addCleanup in the unit tests to avoid stale state if setUp fails or any failure in tearDown method.

There is a bp in cinder project, just copy them here for reference,
"Infra team has indicated that tearDown methods should be replaced with addCleanup in unit tests.
The reason is that all addCleanup methods will be executed even if one of them fails, while a failure in tearDown method can leave the rest of the tearDown un-executed, which can leave stale state laying around.

Moreover, tearDown methods won't run if an exception raises in setUp method, while addCleanup will run in such case.

So, we should replace tearDown with addCleanup methods."

Since the tearDown method is not used widely in keystone sub-project, so just file a bug to track the change."

The link of the reference: https://blueprints.launchpad.net/cinder/+spec/replace-teardown-with-addcleanup

Dave Chen (wei-d-chen)
Changed in keystone:
assignee: nobody → Dave Chen (wei-d-chen)
description: updated
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to keystone (master)

Fix proposed to branch: master
Review: https://review.openstack.org/207753

Changed in keystone:
status: New → In Progress
Revision history for this message
Dave Chen (wei-d-chen) wrote :

Interesting, this poster explained well about the behavior of teardown, http://pythontesting.net/framework/unittest/unittest-wrong-teardown/.

Revision history for this message
Dolph Mathews (dolph) wrote :

FWIW, I don't think it's necessary to file a bug for refactors. I also think it's completely ridiculous to file a blueprint, given that a pure refactor has absolutely zero impact on end users.

tags: added: test-improvement
Changed in keystone:
importance: Undecided → Low
Dave Chen (wei-d-chen)
description: updated
Revision history for this message
Dave Chen (wei-d-chen) wrote :

@Dolph, I agree, this should be just a refactor, it's not worth a BP and not even a bug.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to keystone (master)

Reviewed: https://review.openstack.org/207753
Committed: https://git.openstack.org/cgit/openstack/keystone/commit/?id=6a8825ebb06dec9d5263667570fb85ba26e10647
Submitter: Jenkins
Branch: master

commit 6a8825ebb06dec9d5263667570fb85ba26e10647
Author: Dave Chen <email address hidden>
Date: Fri Jul 31 14:58:10 2015 +0800

    Cleanup tearDown in unit tests

    Infra team has indicated that tearDown should not be used and should
    be replaced with addCleanup in all places.

    Any failure in tearDown methods can leave the rest of the tearDown
    un-executed, which can leave stale state laying around.

    Cleanup items are called even if setUp fails (unlike tearDown).

    - Remove tearDown in test_ldap_livetest.py since it doesn't add any
      functionality.
    - Replace tearDown with addCleanup in test_sql_upgrade.py, and split
      the original tearDown into three parts, so any failure of them won't
      affect other addCleanup methods's execution.

    Change-Id: I6011d066ef928347772313221277cc641e4dd0a3
    Closes-Bug: #1480119

Changed in keystone:
status: In Progress → Fix Committed
Changed in keystone:
milestone: none → liberty-3
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in keystone:
milestone: liberty-3 → 8.0.0
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.