Is there a drawback to using __init __ (self) instead of setting (self) for the nose testing class?

Running nosetests -s for

 class TestTemp(): def __init__(self): print '__init__' self.even = 0 def setup(self): print '__setup__' self.odd = 1 def test_even(self): print 'test_even' even_number = 10 assert even_number % 2 == self.even def test_odd(self): print 'test_odd' odd_number = 11 assert odd_number % 2 == self.odd 

displays the following.

 __init__ __init__ __setup__ test_even .__setup__ test_odd . 

Test instances are created before the tests run, and the installation is performed immediately before the test.

In general, __init__() and setup () do the same thing, but is there a drawback in using __init__() instead of setup ()? Or using both?

+7
source share
2 answers

Although __init__ may work as a replacement for setUp , you should stick with setUp because it is part of a stylized protocol for writing tests. It also has an analogue of tearDown , which does not have __init__ , as well as class and module analogies that __init__ does not support.

Writing test classes is different than writing regular classes, so you must adhere to the style used to write test classes.

+2
source

Yes, you must create a clean list for tests and isolate individual tests.

It seems that test instances (one per test) are created in one batch, and setup is called immediately before each test. If your installation requires a reset external state, you need to do this in setup ; if you must do this in __init__ , individual tests can ruin this external state for the rest of the test run.

+2
source

All Articles