Is there a downside for using __init__(self) instead of setup(self) for a nose test class?

前端 未结 2 1311
天涯浪人
天涯浪人 2021-01-12 12:58

Running nosetests -s for

class TestTemp():

    def __init__(self):
        print \'__init__\'
        self.even = 0

    def setup(self):
              


        
相关标签:
2条回答
  • 2021-01-12 13:42

    While __init__ may work as a replacement for setUp, you should stick to setUp because it is part of the stylized protocol for writing tests. It also has a counterpart, tearDown, which __init__ does not, as well as class- and module-level counterparts which __init__ does not.

    Writing test classes is different than writing normal classes, so you should stick to the style used to write test classes.

    0 讨论(0)
  • 2021-01-12 13:43

    Yes, you are supposed to create a clean slate for the tests, and keep your individual tests isolated.

    It appears the test instances (one per test) are created in one batch, while setup is called right before each test. If your setup needs to reset external state, you'll need to do this in setup; if you were to do this in __init__ individual tests can screw up that external state for the rest of the test run.

    0 讨论(0)
提交回复
热议问题