if condition in setUp() ignore test

為{幸葍}努か 提交于 2019-12-22 14:39:09

问题


in unittest python library, exists the functions setUp and tearDown for set variables and other things pre and post tests.

how I can run or ignore a test with a condition in setUp ?


回答1:


You can call if cond: self.skipTest('reason') in setUp().




回答2:


Instead of checking in setUp, use the skipIf decorator.

@unittest.skipIf(not os.path.exists("somefile.txt"),
                 "somefile.txt is missing")
def test_thing_requiring_somefile(self):
    ...

skipIf can also be used on the class, so you can skip all contained tests if the condition does not hold.

@unittest.skipIf(not os.path.exists("somefile.txt"),
                 "somefile.txt is missing")
class TestStuff(unittest.TestCase):

    def setUp(self):
        ...

    def test_scenario_one(self):
        ...

    def test_scenario_two(self):
        ...


来源:https://stackoverflow.com/questions/23741133/if-condition-in-setup-ignore-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!