如果setUp在Python unittest中引发异常,则停止运行测试

Ida*_*n K 7 python testing

我有这个测试类:

class mytest(unittest.TestCase):
    def setUp(self):
        os.mkdir(...)
        ...

    def tearDown(self):
        shutil.rmtree(...)

    def test_one(self):
        ...

    def test_two(self):
        ...
Run Code Online (Sandbox Code Playgroud)

如果事情失败后,mkdir运行的时候已经跑了setUptest_one,它仍然会尝试运行setUptest_two.此时我会收到错误,mkdir因为rmtree没有运行.

有没有办法告诉Python unittest如果setUp失败就停止运行当前的测试?我打算停止常规的测试失败.

Jim*_*ket 12

在setUp方法中添加失败调用.

def setUp(self):
    try:
        somethingThatMightFail()
    except:
        self.fail()
Run Code Online (Sandbox Code Playgroud)