问题
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