W pętli testowej unittest.TestSuite
, jest to warunek na początku break
:
class TestSuite(BaseTestSuite):
def run(self, result, debug=False):
topLevel = False
if getattr(result, '_testRunEntered', False) is False:
result._testRunEntered = topLevel = True
for test in self:
if result.shouldStop:
break
Więc używam niestandardowego zestawu testowego takiego:
class CustomTestSuite(unittest.TestSuite):
""" This variant registers the test result object with all ScriptedTests,
so that a failed Loign test can abort the test suite by setting result.shouldStop
to True
"""
def run(self, result, debug=False):
for test in self._tests:
test.result = result
return super(CustomTestSuite, self).run(result, debug)
z wynikiem zwyczaj testowym Klasa podobna do tej:
class CustomTestResult(TextTestResult):
def __init__(self, stream, descriptions, verbosity):
super(CustomTestResult, self).__init__(stream, descriptions, verbosity)
self.verbosity = verbosity
self.shouldStop = False
a moje zajęcia testowe są następujące:
class ScriptedTest(unittest.TestCase):
def __init__(self, environment, test_cfg, module, test):
super(ScriptedTest, self).__init__()
self.result = None
Pod pewnymi warunkami przerywam pakiet testowy; Na przykład, zestaw testowy rozpoczyna logowania, a jeśli to się nie powiedzie, nie muszę spróbować reszty:
try:
test_case.execute_script(test_command_list)
except AssertionError as e:
if test_case.module == 'session' and test_case.test == 'Login':
test_case.result.shouldStop = True
raise TestFatal('Login failed, aborting test.')
else:
raise sys.exc_info()
Potem korzystania z zestawu testowego w następujący sposób:
suite = CustomTestSuite()
self.add_tests(suite)
result = unittest.TextTestRunner(verbosity=self.environment.verbosity, stream=UnitTestLoggerStream(self.logger),
resultclass=CustomTestResult).run(suite)
Nie jestem pewien, czy jest lepszy sposób, aby to zrobić, ale zachowuje się poprawnie dla moich testów.
Używam tego, aby przerwać przebieg testu przed uruchomieniem jakichkolwiek testów, jeśli testowana aplikacja jest skonfigurowana z ustawieniami prod. (w rzeczywistości z dowolnymi ustawieniami innymi niż testowe.) –