How to use __setup_extra_class_teardowns method in Testify

Best Python code snippet using Testify_python

test_case_test.py

Source:test_case_test.py Github

copy

Full Screen

1import mock2import unittest3from testify import assert_equal4from testify import assert_in5from testify import class_setup6from testify import class_setup_teardown7from testify import class_teardown8from testify import let9from testify import run10from testify import setup11from testify import teardown12from testify import TestCase13from testify.test_case import TestifiedUnitTest14class TestMethodsGetRun(TestCase):15 def test_method_1(self):16 self.test_1_run = True17 def test_method_2(self):18 self.test_2_run = True19 @class_teardown20 def assert_test_methods_were_run(self):21 assert self.test_1_run22 assert self.test_2_run23class DeprecatedClassSetupFixturesGetRun(TestCase):24 def classSetUp(self):25 self.test_var = True26 def test_test_var(self):27 assert self.test_var28class DeprecatedSetupFixturesGetRun(TestCase):29 def setUp(self):30 self.test_var = True31 def test_test_var(self):32 assert self.test_var33class DeprecatedTeardownFixturesGetRun(TestCase):34 COUNTER = 035 def tearDown(self):36 self.test_var = True37 def test_test_var_pass_1(self):38 self.COUNTER += 139 if self.COUNTER > 1:40 assert self.test_var41 def test_test_var_pass_2(self):42 self.COUNTER += 143 if self.COUNTER > 1:44 assert self.test_var45class DeprecatedClassTeardownFixturesGetRun(TestCase):46 def test_placeholder(self):47 pass48 def class_teardown(self):49 self.test_var = True50 @class_teardown51 def test_test_var(self):52 assert self.test_var53class ClassSetupFixturesGetRun(TestCase):54 @class_setup55 def set_test_var(self):56 self.test_var = True57 def test_test_var(self):58 assert self.test_var59class SetupFixturesGetRun(TestCase):60 @setup61 def set_test_var(self):62 self.test_var = True63 def test_test_var(self):64 assert self.test_var65class TeardownFixturesGetRun(TestCase):66 COUNTER = 067 @teardown68 def set_test_var(self):69 self.test_var = True70 def test_test_var_first_pass(self):71 self.COUNTER += 172 if self.COUNTER > 1:73 assert self.test_var74 def test_test_var_second_pass(self):75 self.COUNTER += 176 if self.COUNTER > 1:77 assert self.test_var78class OverrideTest(TestCase):79 def test_method_1(self):80 pass81 def test_method_2(self):82 pass83class UnitTest(unittest.TestCase):84 # a compact way to record each step's completion85 status = [False] * 686 def classSetUp(self):87 self.status[0] = True88 def setUp(self):89 self.status[1] = True90 def test_i_ran(self):91 self.status[2] = True92 def tearDown(self):93 self.status[3] = True94 def classTearDown(self):95 self.status[4] = True96 @teardown97 def no_really_i_tore_down(self):98 """Fixture mixins should still work as expected."""99 self.status[5] = True100class UnitTestTestYoDawg(TestCase):101 """Make sure we actually detect and run all steps in unittest.TestCases."""102 def test_unit_test_status(self):103 TestifiedUnitTest.from_unittest_case(UnitTest)().run()104 assert_equal(UnitTest.status, [True] * 6)105# The following cases test unittest.TestCase inheritance, fixtures and mixins106class BaseUnitTest(unittest.TestCase):107 done = False108 def __init__(self):109 super(BaseUnitTest, self).__init__()110 self.init = True111 def setUp(self):112 assert self.init113 assert not self.done114 self.foo = True115 def tearDown(self):116 assert self.init117 assert not self.done118 self.done = True119class DoNothingMixin(object):120 pass121class DerivedUnitTestMixinWithFixture(BaseUnitTest):122 @setup123 def set_bar(self):124 assert self.foo # setUp runs first125 self.bar = True126 @teardown127 def not_done(self): # tearDown runs last128 assert not self.done129 @class_teardown130 def i_ran(cls):131 cls.i_ran = True132class DerivedUnitTestWithFixturesAndTests(DerivedUnitTestMixinWithFixture, DoNothingMixin):133 def test_foo_bar(self):134 assert self.foo135 assert self.bar136 assert not self.done137class DerivedUnitTestWithAdditionalFixturesAndTests(DerivedUnitTestMixinWithFixture):138 @setup139 def set_baz(self):140 assert self.foo141 assert self.bar142 self.baz = True143 @teardown144 def clear_foo(self):145 self.foo = False146 def test_foo_bar_baz(self):147 assert self.foo148 assert self.bar149 assert self.baz150class TestDerivedUnitTestsRan(TestCase):151 def test_unit_tests_ran(self):152 assert DerivedUnitTestMixinWithFixture.i_ran153 assert DerivedUnitTestWithFixturesAndTests.i_ran154 assert DerivedUnitTestWithAdditionalFixturesAndTests.i_ran155class ClobberLetTest(TestCase):156 """Test overwritting a let does not break subsequent tests.157 Because we are unsure which test will run first, two tests will clobber a158 let that is asserted about in the other test.159 """160 @let161 def something(self):162 return 1163 @let164 def something_else(self):165 return 2166 def test_something(self):167 self.something_else = 3168 assert_equal(self.something, 1)169 def test_something_else(self):170 self.something = 4171 assert_equal(self.something_else, 2)172class TestResultDataTest(TestCase):173 """ Tests the data returned by Testify for test-results have required174 arguments/parameters or not. """175 def test_testresult_started(self):176 class InnerTestCase(TestCase):177 pass178 class TestCaseCallback(object):179 def __init__(self):180 pass181 def __call__(self, result_dict):182 self.result_dict = result_dict183 run_test_case_callback = TestCaseCallback()184 complete_test_case_callback = TestCaseCallback()185 inner_test_case = InnerTestCase()186 inner_test_case.register_callback(TestCase.EVENT_ON_RUN_TEST_CASE, run_test_case_callback)187 inner_test_case.register_callback(TestCase.EVENT_ON_COMPLETE_TEST_CASE, complete_test_case_callback)188 inner_test_case.run()189 assert 'start_time' in run_test_case_callback.result_dict190 assert run_test_case_callback.result_dict['end_time'] is None191 assert complete_test_case_callback.result_dict['end_time'] is not None192 assert complete_test_case_callback.result_dict['run_time'] is not None193 assert complete_test_case_callback.result_dict['method']['module'] is not None194 assert complete_test_case_callback.result_dict['method']['full_name'] is not None195 assert complete_test_case_callback.result_dict['method']['class'] is not None196class CallbacksGetCalledTest(TestCase):197 def test_class_fixtures_get_reported(self):198 """Make a test case, register a bunch of callbacks for class fixtures199 on it, and make sure the callbacks are all run in the right order.200 """201 class InnerTestCase(TestCase):202 def classSetUp(self):203 pass204 def classTearDown(self):205 pass206 @class_setup_teardown207 def __class_setup_teardown(self):208 yield209 def test_things(self):210 pass211 inner_test_case = InnerTestCase()212 events = (213 TestCase.EVENT_ON_RUN_TEST_METHOD,214 TestCase.EVENT_ON_COMPLETE_TEST_METHOD,215 TestCase.EVENT_ON_RUN_CLASS_SETUP_METHOD,216 TestCase.EVENT_ON_COMPLETE_CLASS_SETUP_METHOD,217 TestCase.EVENT_ON_RUN_CLASS_TEARDOWN_METHOD,218 TestCase.EVENT_ON_COMPLETE_CLASS_TEARDOWN_METHOD,219 TestCase.EVENT_ON_RUN_TEST_CASE,220 TestCase.EVENT_ON_COMPLETE_TEST_CASE,221 )222 calls_to_callback = []223 def make_callback(event):224 def callback(result):225 calls_to_callback.append((event, result['method']['name'] if result else None))226 return callback227 for event in events:228 inner_test_case.register_callback(event, make_callback(event))229 inner_test_case.run()230 assert_equal(calls_to_callback, [231 (TestCase.EVENT_ON_RUN_TEST_CASE, 'run'),232 (TestCase.EVENT_ON_RUN_CLASS_SETUP_METHOD, '__setup_extra_class_teardowns'),233 (TestCase.EVENT_ON_COMPLETE_CLASS_SETUP_METHOD, '__setup_extra_class_teardowns'),234 (TestCase.EVENT_ON_RUN_CLASS_SETUP_METHOD, 'classSetUp'),235 (TestCase.EVENT_ON_COMPLETE_CLASS_SETUP_METHOD, 'classSetUp'),236 (TestCase.EVENT_ON_RUN_CLASS_SETUP_METHOD, '__class_setup_teardown'),237 (TestCase.EVENT_ON_COMPLETE_CLASS_SETUP_METHOD, '__class_setup_teardown'),238 (TestCase.EVENT_ON_RUN_TEST_METHOD, 'test_things'),239 (TestCase.EVENT_ON_COMPLETE_TEST_METHOD, 'test_things'),240 (TestCase.EVENT_ON_RUN_CLASS_TEARDOWN_METHOD, '__class_setup_teardown'),241 (TestCase.EVENT_ON_COMPLETE_CLASS_TEARDOWN_METHOD, '__class_setup_teardown'),242 (TestCase.EVENT_ON_RUN_CLASS_TEARDOWN_METHOD, 'classTearDown'),243 (TestCase.EVENT_ON_COMPLETE_CLASS_TEARDOWN_METHOD, 'classTearDown'),244 (TestCase.EVENT_ON_RUN_CLASS_TEARDOWN_METHOD, '__setup_extra_class_teardowns'),245 (TestCase.EVENT_ON_COMPLETE_CLASS_TEARDOWN_METHOD, '__setup_extra_class_teardowns'),246 (TestCase.EVENT_ON_COMPLETE_TEST_CASE, 'run'),247 ])248class FailingTeardownMethodsTest(TestCase):249 class ClassWithTwoFailingTeardownMethods(TestCase):250 methods_ran = []251 def test_method(self):252 self.methods_ran.append("test_method")253 assert False254 @teardown255 def first_teardown(self):256 self.methods_ran.append("first_teardown")257 assert False258 @teardown259 def second_teardown(self):260 self.methods_ran.append("second_teardown")261 assert False262 @setup263 def run_test_case(self):264 self.testcase = self.ClassWithTwoFailingTeardownMethods()265 self.testcase.run()266 def test_class_with_two_failing_teardown_methods(self):267 assert_in("test_method", self.testcase.methods_ran)268 assert_in("first_teardown", self.testcase.methods_ran)269 assert_in("second_teardown", self.testcase.methods_ran)270 def test_multiple_error_formatting(self):271 test_result = self.testcase.results()[0]272 assert_equal(273 test_result.format_exception_info().split('\n'),274 [275 'Traceback (most recent call last):',276 RegexMatcher(r' File "(\./)?test/test_case_test\.py", line \d+, in test_method'),277 ' assert False',278 'AssertionError',279 '',280 'During handling of the above exception, another exception occurred:',281 '',282 'Traceback (most recent call last):',283 RegexMatcher(r' File "(\./)?test/test_case_test\.py", line \d+, in first_teardown'),284 ' assert False',285 'AssertionError',286 '',287 'During handling of the above exception, another exception occurred:',288 '',289 'Traceback (most recent call last):',290 RegexMatcher(r' File "(\./)?test/test_case_test\.py", line \d+, in second_teardown'),291 ' assert False',292 'AssertionError',293 '', # Ends with newline.294 ]295 )296class RegexMatcher(object):297 def __init__(self, regex):298 import re299 self.__re = re.compile(regex)300 def __eq__(self, other):301 return bool(self.__re.match(other))302 def __repr__(self):303 return '%s(%r)' % (304 type(self).__name__,305 self.__re.pattern,306 )307class ExceptionDuringClassSetupTest(TestCase):308 class FakeParentTestCase(TestCase):309 def __init__(self, *args, **kwargs):310 self.run_methods = []311 super(ExceptionDuringClassSetupTest.FakeParentTestCase, self).__init__(*args, **kwargs)312 @class_setup313 def parent_class_setup(self):314 self.run_methods.append("parent class_setup")315 raise Exception316 @class_teardown317 def parent_class_teardown(self):318 self.run_methods.append("parent class_teardown")319 @setup320 def parent_setup(self):321 self.run_methods.append("parent setup")322 raise Exception323 @teardown324 def parent_teardown(self):325 self.run_methods.append("parent teardown")326 def test_parent(self):327 self.run_methods.append("parent test method")328 class FakeChildTestCase(FakeParentTestCase):329 @class_setup330 def child_class_setup(self):331 self.run_methods.append("child class_setup")332 @class_teardown333 def child_class_teardown(self):334 self.run_methods.append("child class_teardown")335 @setup336 def child_setup(self):337 self.run_methods.append("child setup")338 @teardown339 def child_teardown(self):340 self.run_methods.append("child teardown")341 def test_child(self):342 self.run_methods.append("child test method")343 def test_parent(self):344 test_case = self.FakeParentTestCase()345 test_case.run()346 expected = ["parent class_setup", "parent class_teardown", ]347 assert_equal(expected, test_case.run_methods)348 def test_child(self):349 test_case = self.FakeChildTestCase()350 test_case.run()351 expected = ["parent class_setup", "child class_teardown", "parent class_teardown", ]352 assert_equal(expected, test_case.run_methods)353class ExceptionDuringSetupTest(TestCase):354 class FakeParentTestCase(TestCase):355 def __init__(self, *args, **kwargs):356 self.run_methods = []357 super(ExceptionDuringSetupTest.FakeParentTestCase, self).__init__(*args, **kwargs)358 @setup359 def parent_setup(self):360 self.run_methods.append("parent setup")361 raise Exception362 @teardown363 def parent_teardown(self):364 self.run_methods.append("parent teardown")365 def test_parent(self):366 self.run_methods.append("parent test method")367 class FakeChildTestCase(FakeParentTestCase):368 @setup369 def child_setup(self):370 self.run_methods.append("child setup")371 @teardown372 def child_teardown(self):373 self.run_methods.append("child teardown")374 def test_child(self):375 self.run_methods.append("child test method")376 def test_parent(self):377 test_case = self.FakeParentTestCase()378 test_case.run()379 expected = ["parent setup", "parent teardown", ]380 assert_equal(expected, test_case.run_methods)381 def test_child(self):382 test_case = self.FakeChildTestCase()383 test_case.run()384 # FakeChildTestCase has two test methods (test_parent and test_child), so the fixtures are run twice.385 expected = ["parent setup", "child teardown", "parent teardown", ] * 2386 assert_equal(expected, test_case.run_methods)387class ExceptionDuringClassTeardownTest(TestCase):388 class FakeParentTestCase(TestCase):389 def __init__(self, *args, **kwargs):390 self.run_methods = []391 super(ExceptionDuringClassTeardownTest.FakeParentTestCase, self).__init__(*args, **kwargs)392 @class_setup393 def parent_setup(self):394 self.run_methods.append("parent class_setup")395 @class_teardown396 def parent_teardown(self):397 self.run_methods.append("parent class_teardown")398 raise Exception399 def test_parent(self):400 self.run_methods.append("parent test method")401 class FakeChildTestCase(FakeParentTestCase):402 @class_setup403 def child_setup(self):404 self.run_methods.append("child class_setup")405 @class_teardown406 def child_teardown(self):407 self.run_methods.append("child class_teardown")408 def test_child(self):409 self.run_methods.append("child test method")410 def test_parent(self):411 test_case = self.FakeParentTestCase()412 test_case.run()413 expected = ["parent class_setup", "parent test method", "parent class_teardown", ]414 assert_equal(expected, test_case.run_methods)415 def test_child(self):416 test_case = self.FakeChildTestCase()417 test_case.run()418 expected = [419 "parent class_setup",420 "child class_setup",421 "child test method",422 "parent test method",423 "child class_teardown",424 "parent class_teardown",425 ]426 assert_equal(expected, test_case.run_methods)427class ExceptionDuringTeardownTest(TestCase):428 class FakeParentTestCase(TestCase):429 def __init__(self, *args, **kwargs):430 self.run_methods = []431 super(ExceptionDuringTeardownTest.FakeParentTestCase, self).__init__(*args, **kwargs)432 @setup433 def parent_setup(self):434 self.run_methods.append("parent setup")435 @teardown436 def parent_teardown(self):437 self.run_methods.append("parent teardown")438 raise Exception439 def test_parent(self):440 self.run_methods.append("parent test method")441 class FakeChildTestCase(FakeParentTestCase):442 @setup443 def child_setup(self):444 self.run_methods.append("child setup")445 @teardown446 def child_teardown(self):447 self.run_methods.append("child teardown")448 def test_child(self):449 self.run_methods.append("child test method")450 def test_parent(self):451 test_case = self.FakeParentTestCase()452 test_case.run()453 expected = ["parent setup", "parent test method", "parent teardown", ]454 assert_equal(expected, test_case.run_methods)455 def test_child(self):456 test_case = self.FakeChildTestCase()457 test_case.run()458 expected = [459 # Fixtures run before and after each test method.460 # Here's test_child.461 "parent setup",462 "child setup",463 "child test method",464 "child teardown",465 "parent teardown",466 # Here's test_parent.467 "parent setup",468 "child setup",469 "parent test method",470 "child teardown",471 "parent teardown",472 ]473 assert_equal(expected, test_case.run_methods)474class TestCaseKeepsReferenceToResultsForTestMethod(TestCase):475 def test_reference_to_results(self):476 assert self.test_result477class NoAttributesNamedTest(TestCase):478 class FakeTestCase(TestCase):479 def test_your_might(self):480 assert True481 def test_attributes(self):482 test_case = self.FakeTestCase()483 expected_attributes = sorted([484 "test_result", # Part of the public API (its name is unfortunate but e.g. Selenium relies on it)485 "test_your_might", # "Actual" test method in the test case486 ])487 actual_attributes = sorted([attribute for attribute in dir(test_case) if attribute.startswith("test")])488 assert_equal(expected_attributes, actual_attributes)489class AdhocTeardownsGetCalledTest(TestCase):490 def test_addfinalizer(self):491 class_setup_mock = mock.MagicMock()492 test_setup_mock = mock.MagicMock()493 test_mock = mock.MagicMock()494 test_teardown_mock = mock.MagicMock()495 class_teardown_mock = mock.MagicMock()496 class InnerTestCase(TestCase):497 @class_setup498 def _setup_class_mocks(self):499 self.addfinalizer(class_setup_mock)500 @setup501 def _setup_test_mock(self):502 self.addfinalizer(test_setup_mock)503 def test_things(self):504 self.addfinalizer(test_mock)505 assert not class_setup_mock.called506 assert not test_setup_mock.called507 assert not test_mock.called508 @teardown509 def _test_teardown(self):510 self.addfinalizer(test_teardown_mock)511 # The test instance teardowns run at the end of the test512 assert not class_setup_mock.called513 assert not test_setup_mock.called514 assert not test_mock.called515 assert not test_teardown_mock.called516 @class_teardown517 def _test_class_teardown(self):518 self.addfinalizer(class_teardown_mock)519 # The class teardowns run at the end of the tests520 assert not class_setup_mock.called521 assert not class_teardown_mock.called522 assert_equal(test_setup_mock.call_count, 1)523 assert_equal(test_mock.call_count, 1)524 assert_equal(test_teardown_mock.call_count, 1)525 test_case = InnerTestCase()526 test_case.run()527 assert_equal(test_case.results()[0].format_exception_info(), None)528 assert_equal(test_setup_mock.call_count, 1)529 assert_equal(test_mock.call_count, 1)530 assert_equal(test_teardown_mock.call_count, 1)531 assert_equal(class_setup_mock.call_count, 1)532 assert_equal(class_teardown_mock.call_count, 1)533 def test_multiple_tests(self):534 class_setup_mock = mock.MagicMock()535 test_setup_mock = mock.MagicMock()536 test_mock_1 = mock.MagicMock()537 test_mock_2 = mock.MagicMock()538 test_teardown_mock = mock.MagicMock()539 class_teardown_mock = mock.MagicMock()540 class InnerTestCase(TestCase):541 @class_setup542 def _setup_class_mocks(self):543 self.addfinalizer(class_setup_mock)544 @setup545 def _setup_test_mock(self):546 self.addfinalizer(test_setup_mock)547 def test_1(self):548 self.addfinalizer(test_mock_1)549 assert not class_setup_mock.called550 assert not test_setup_mock.called551 assert not test_mock_1.called552 assert not test_mock_2.called553 def test_2(self):554 self.addfinalizer(test_mock_2)555 assert_equal(test_mock_1.call_count, 1)556 assert not class_setup_mock.called557 assert not test_setup_mock.called558 assert not test_mock_2.called559 @teardown560 def _test_teardown(self):561 self.addfinalizer(test_teardown_mock)562 assert not class_setup_mock.called563 @class_teardown564 def _test_class_teardown(self):565 self.addfinalizer(class_teardown_mock)566 assert not class_setup_mock.called567 assert not class_teardown_mock.called568 assert_equal(test_setup_mock.call_count, 2)569 assert_equal(test_mock_1.call_count, 1)570 assert_equal(test_mock_2.call_count, 1)571 assert_equal(test_teardown_mock.call_count, 2)572 test_case = InnerTestCase()573 test_case.run()574 assert_equal(test_case.results()[0].format_exception_info(), None)575 assert_equal(test_setup_mock.call_count, 2)576 assert_equal(test_teardown_mock.call_count, 2)577 assert_equal(class_setup_mock.call_count, 1)578 assert_equal(class_teardown_mock.call_count, 1)579 assert_equal(test_mock_1.call_count, 1)580 assert_equal(test_mock_2.call_count, 1)581 def test_individual_test_teardowns_ordering(self):582 mock_1 = mock.MagicMock()583 mock_2 = mock.MagicMock()584 def finalizer_1():585 assert not mock_1.called586 assert mock_2.called587 mock_1()588 def finalizer_2():589 assert not mock_1.called590 assert not mock_2.called591 mock_2()592 class InnerTestCase(TestCase):593 def test_things(self):594 self.addfinalizer(finalizer_1)595 self.addfinalizer(finalizer_2)596 test_case = InnerTestCase()597 test_case.run()598 assert_equal(test_case.results()[0].format_exception_info(), None)599 assert_equal(mock_1.call_count, 1)600 assert_equal(mock_2.call_count, 1)601 def test_class_teardowns_ordering(self):602 mock_1 = mock.MagicMock()603 mock_2 = mock.MagicMock()604 def finalizer_1():605 assert not mock_1.called606 assert mock_2.called607 mock_1()608 def finalizer_2():609 assert not mock_1.called610 assert not mock_2.called611 mock_2()612 class InnerTestCase(TestCase):613 @class_setup614 def _class_setup(self):615 self.addfinalizer(finalizer_1)616 self.addfinalizer(finalizer_2)617 def test_things(self):618 pass619 test_case = InnerTestCase()620 test_case.run()621 assert_equal(test_case.results()[0].format_exception_info(), None)622 assert_equal(mock_1.call_count, 1)623 assert_equal(mock_2.call_count, 1)624if __name__ == '__main__':625 run()...

Full Screen

Full Screen

test_case.py

Source:test_case.py Github

copy

Full Screen

...262 self.__extra_class_teardowns.append(teardown_func)263 else:264 raise RuntimeError('Tried to add a teardown while the test was not being executed.')265 @test_fixtures.class_setup_teardown266 def __setup_extra_class_teardowns(self):267 self.__extra_class_teardowns = []268 yield269 for teardown in reversed(self.__extra_class_teardowns):270 teardown()271 @test_fixtures.setup_teardown272 def __setup_extra_test_teardowns(self):273 self.__extra_test_teardowns = []274 yield275 for teardown in reversed(self.__extra_test_teardowns):276 teardown()277 def register_callback(self, event, callback):278 """Register a callback for an internal event, usually used for logging.279 The argument to the callback will be the test method object itself.280 Fixture objects can be distinguished by the running them through...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testify automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful