nose-devs / nose

nose is nicer testing for python
http://readthedocs.org/docs/nose/en/latest/
1.36k stars 395 forks source link

Teardown runs twice #1066

Closed danjamesmay closed 6 years ago

danjamesmay commented 6 years ago

I am writing nose test functions in a file.

I am calling nosetests -s -v && nosetests --version to activate the test runner.

My test code is as follows:

def setup():
    print("I AM SETTING UP")

def teardown():
    print("I AM TEARING DOWN")

@with_setup(setup=setup, teardown=teardown)
def test_tearing_down():
    assert_equals(1,1)

Output:

I AM SETTING UP
I AM TEARING DOWN
I AM TEARING DOWN
nosetests version 1.3.7

Tried with versions: 1.3.0, 1.1.0, 1.0.0. Same output.

What on earth is going on?

I originally was trying to use a teardown method to rename a database back to its original name after it is renamed by the test. This fault injection mechanism was to test how the client reacts when it can't connect to my database. This was failing because the teardown was running twice.

Now I have moved this teardown to the bottom of the test, so it is not guaranteed to run, which is not ideal.

jszakmeister commented 6 years ago

First, your output doesn't look right. I get:

I AM SETTING UP
mytest.test_tearing_down ... I AM SETTING UP
I AM TEARING DOWN
ok
I AM TEARING DOWN

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

You're running into this because of how you named the functions. setup and teardown are "reserved" for module-level setup and teardown of test fixtures. Then you have a test case that then uses those function as a setup and teardown. The combined effect that both setup and teardown are being called twice.

Rename the functions to something else (see the link above), and you should be good.