如何在指定测试失败后强制测试停止 运行 测试套件?
How to force tests to stop running a test suite after a specified test failed?
我有一个用 Selenium Webdriver/Python 2.7 编写的测试套件,其中包含几个测试用例。一些测试用例非常关键,如果它们失败,则整个测试都会失败,之后就不需要 运行 个测试用例了。
class TestSuite1(unittest.TestCase)
def setUp(self):
pass
def test1(self):
return True
def test2(self):
return false
def test3(self):
return True
# This is a critical test
def test4(self):
return false
def test5(self):
return True
def tearDown(self):
pass
所以,我想在 test4 失败时停止整个测试 运行(当 test2 失败时,测试 运行 应该继续)因为它很关键。我知道我们可以使用装饰器,但我正在寻找一种更有效的方法,因为我的测试中有大约 20 个关键测试 运行,并且对所有测试用例使用 20 个标志似乎效率不高。
像这样的东西怎么样:
import unittest
class CustomResult(unittest.TestResult):
def addFailure(self, test, err):
critical = ['test4', 'test7']
if test._testMethodName in critical:
print("Critical Failure!")
self.stop()
unittest.TestResult.addFailure(self, test, err)
class TestSuite1(unittest.TestCase):
def setUp(self):
pass
def test1(self):
return True
def test2(self):
return False
def test3(self):
return True
# This is a critical test
def test4(self):
self.fail()
pass
def test5(self):
print("test5")
return True
def tearDown(self):
pass
if __name__ == '__main__':
runner = unittest.runner.TextTestRunner(resultclass=CustomResult)
unittest.main(testRunner=runner)
您可能需要根据调用测试的方式进行调整。
如果self.fail()
(在test4
中)被注释掉,则测试5个方法。但如果它没有被注释掉,测试会打印 "Critical Failure!" 并停止。就我而言,只有 4 个测试 运行.
命名这些方法也可能很聪明,这样当按字典顺序排序时,它们排在第一位,这样如果发生严重故障,就不会浪费时间测试其他方法。
输出(self.fail()
):
Critical Failure!
Ran 4 tests in 0.001s
FAILED (failures=1)
输出(没有self.fail()
):
test5
Ran 5 tests in 0.001s
OK
我有一个用 Selenium Webdriver/Python 2.7 编写的测试套件,其中包含几个测试用例。一些测试用例非常关键,如果它们失败,则整个测试都会失败,之后就不需要 运行 个测试用例了。
class TestSuite1(unittest.TestCase)
def setUp(self):
pass
def test1(self):
return True
def test2(self):
return false
def test3(self):
return True
# This is a critical test
def test4(self):
return false
def test5(self):
return True
def tearDown(self):
pass
所以,我想在 test4 失败时停止整个测试 运行(当 test2 失败时,测试 运行 应该继续)因为它很关键。我知道我们可以使用装饰器,但我正在寻找一种更有效的方法,因为我的测试中有大约 20 个关键测试 运行,并且对所有测试用例使用 20 个标志似乎效率不高。
像这样的东西怎么样:
import unittest
class CustomResult(unittest.TestResult):
def addFailure(self, test, err):
critical = ['test4', 'test7']
if test._testMethodName in critical:
print("Critical Failure!")
self.stop()
unittest.TestResult.addFailure(self, test, err)
class TestSuite1(unittest.TestCase):
def setUp(self):
pass
def test1(self):
return True
def test2(self):
return False
def test3(self):
return True
# This is a critical test
def test4(self):
self.fail()
pass
def test5(self):
print("test5")
return True
def tearDown(self):
pass
if __name__ == '__main__':
runner = unittest.runner.TextTestRunner(resultclass=CustomResult)
unittest.main(testRunner=runner)
您可能需要根据调用测试的方式进行调整。
如果self.fail()
(在test4
中)被注释掉,则测试5个方法。但如果它没有被注释掉,测试会打印 "Critical Failure!" 并停止。就我而言,只有 4 个测试 运行.
命名这些方法也可能很聪明,这样当按字典顺序排序时,它们排在第一位,这样如果发生严重故障,就不会浪费时间测试其他方法。
输出(self.fail()
):
Critical Failure! Ran 4 tests in 0.001s FAILED (failures=1)
输出(没有self.fail()
):
test5 Ran 5 tests in 0.001s OK