def _replay_suite_events(suite, eventmgr): # type: (SuiteResult, BaseEventManager) -> None eventmgr.fire(events.SuiteStartEvent(suite, suite.start_time)) if suite.suite_setup: eventmgr.fire( events.SuiteSetupStartEvent(suite, suite.suite_setup.start_time)) _replay_steps_events(TreeLocation.in_suite_setup(suite), suite.suite_setup.steps, eventmgr) if suite.suite_setup.end_time: eventmgr.fire( events.SuiteSetupEndEvent(suite, suite.suite_setup.end_time)) for test in suite.get_tests(): _replay_test_events(test, eventmgr) for sub_suite in suite.get_suites(): _replay_suite_events(sub_suite, eventmgr) if suite.suite_teardown: eventmgr.fire( events.SuiteTeardownStartEvent(suite, suite.suite_teardown.start_time)) _replay_steps_events(TreeLocation.in_suite_teardown(suite), suite.suite_teardown.steps, eventmgr) if suite.suite_teardown.end_time: eventmgr.fire( events.SuiteTeardownEndEvent(suite, suite.suite_teardown.end_time)) if suite.end_time: eventmgr.fire(events.SuiteEndEvent(suite, suite.end_time))
def replay_report_events(report, eventmgr): # type: (Report, BaseEventManager) -> None eventmgr.fire(events.TestSessionStartEvent(report, report.start_time)) if report.test_session_setup: eventmgr.fire( events.TestSessionSetupStartEvent( report.test_session_setup.start_time)) _replay_steps_events(TreeLocation.in_test_session_setup(), report.test_session_setup.steps, eventmgr) if report.test_session_setup.end_time: eventmgr.fire( events.TestSessionSetupEndEvent( report.test_session_setup.end_time)) for suite in report.get_suites(): _replay_suite_events(suite, eventmgr) if report.test_session_teardown: eventmgr.fire( events.TestSessionTeardownStartEvent( report.test_session_teardown.start_time)) _replay_steps_events(TreeLocation.in_test_session_teardown(), report.test_session_teardown.steps, eventmgr) if report.test_session_teardown.end_time: eventmgr.fire( events.TestSessionTeardownEndEvent( report.test_session_teardown.end_time)) if report.end_time: eventmgr.fire(events.TestSessionEndEvent(report, report.end_time))
def run(self, context): setup_teardown_funcs = self.scheduled_fixtures.get_setup_teardown_pairs() if any(setup for setup, _ in setup_teardown_funcs): # before actual setup context.event_manager.fire(events.TestSessionSetupStartEvent()) set_runtime_location(TreeLocation.in_test_session_setup()) set_step("Setup test session") # actual setup self.teardown_funcs = context.run_setup_funcs(setup_teardown_funcs, TreeLocation.in_test_session_setup()) # after actual setup context.event_manager.fire(events.TestSessionSetupEndEvent()) if not is_location_successful(TreeLocation.in_test_session_setup()): raise TaskFailure("test session setup failed") else: self.teardown_funcs = [teardown for _, teardown in setup_teardown_funcs if teardown]
def run(self, context): if any(setup for setup, _ in self.setup_teardown_funcs): # before actual initialization context.event_manager.fire(events.SuiteSetupStartEvent(self.suite)) set_runtime_location(TreeLocation.in_suite_setup(self.suite)) set_step("Setup suite") # actual initialization self.teardown_funcs = context.run_setup_funcs( self.setup_teardown_funcs, TreeLocation.in_suite_setup(self.suite) ) # after actual initialization context.event_manager.fire(events.SuiteSetupEndEvent(self.suite)) if not is_location_successful(TreeLocation.in_suite_setup(self.suite)): raise TaskFailure("suite '%s' setup failed" % self.suite.path) else: self.teardown_funcs = [teardown for _, teardown in self.setup_teardown_funcs if teardown]
def run(self, context): if any(self.test_session_setup_task.teardown_funcs): # before actual teardown context.event_manager.fire(events.TestSessionTeardownStartEvent()) set_runtime_location(TreeLocation.in_test_session_teardown()) set_step("Teardown test session") # actual teardown context.run_teardown_funcs(self.test_session_setup_task.teardown_funcs) # after actual teardown context.event_manager.fire(events.TestSessionTeardownEndEvent())
def run(self, context): if any(self.suite_setup_task.teardown_funcs): # before actual teardown context.event_manager.fire(events.SuiteTeardownStartEvent(self.suite)) set_runtime_location(TreeLocation.in_suite_teardown(self.suite)) set_step("Teardown suite") # actual teardown context.run_teardown_funcs(self.suite_setup_task.teardown_funcs) # after actual teardown context.event_manager.fire(events.SuiteTeardownEndEvent(self.suite))
def test_teardown_suite_error_because_of_error_log(): @lcc.suite("MySuite") class MySuite: @lcc.test("Some test") def sometest(self): pass def teardown_suite(self): lcc.log_error("some error") report = run_suite_class(MySuite) assert_test_passed(report) assert_report_node_success(report, TreeLocation.in_suite_teardown("MySuite"), expected=False)
def _replay_test_events(test, eventmgr): # type: (TestResult, BaseEventManager) -> None if test.status in ("passed", "failed", None): # None means "in progress" eventmgr.fire(events.TestStartEvent(test, test.start_time)) _replay_steps_events(TreeLocation.in_test(test), test.steps, eventmgr) if test.end_time: eventmgr.fire(events.TestEndEvent(test, test.end_time)) elif test.status == "skipped": eventmgr.fire( events.TestSkippedEvent(test, test.status_details, test.start_time)) elif test.status == "disabled": eventmgr.fire( events.TestDisabledEvent(test, test.status_details, test.start_time)) else: raise LemonCheesecakeInternalError("Unknown test status '%s'" % test.status)
def test_setup_test_session_error_because_of_exception(): @lcc.fixture(scope="session") def fixt(): 1 / 0 @lcc.suite("MySuite") class MySuite: @lcc.test("Some test") def sometest(self, fixt): pass @lcc.test("Some other test") def sometest_bis(self): pass report = run_suite_class(MySuite, fixtures=[fixt]) assert_test_statuses(report, skipped=["MySuite.sometest", "MySuite.sometest_bis"]) assert_report_node_success(report, TreeLocation.in_test_session_setup(), expected=False)
def test_setup_test_session_error_and_setup_suite(): marker = [] @lcc.suite("MySuite") class MySuite: def setup_suite(self, fixt): marker.append("setup_suite") @lcc.test("Some test") def sometest(self): pass @lcc.fixture(scope="session") def fixt(): 1 / 0 report = run_suite_class(MySuite, fixtures=[fixt]) assert_test_skipped(report) assert_report_node_success(report, TreeLocation.in_test_session_setup(), expected=False) assert not marker
def test_teardown_suite_error_because_of_fixture(): marker = [] @lcc.fixture(scope="suite") def fix(): yield 2 1 / 0 @lcc.suite("MySuite") class MySuite: @lcc.test("Some test") def sometest(self, fix): pass def teardown_suite(self): marker.append("teardown") report = run_suite_class(MySuite, fixtures=[fix]) assert_test_passed(report) assert_report_node_success(report, TreeLocation.in_suite_teardown("MySuite"), expected=False) assert len(marker) == 1
def teardown_test_wrapper(): status_so_far = "passed" if is_location_successful(TreeLocation.in_test(self.test)) else "failed" suite.get_hook("teardown_test")(self.test, status_so_far)
def run(self, context): suite = self.test.parent_suite ### # Checker whether the test must be executed or not ### if self.test.is_disabled() and not context.force_disabled: context.event_manager.fire(events.TestDisabledEvent(self.test, "")) return ### # Begin test ### context.event_manager.fire(events.TestStartEvent(self.test)) set_runtime_location(TreeLocation.in_test(self.test)) ### # Setup test (setup and fixtures) ### setup_teardown_funcs = list() if suite.has_hook("setup_test"): def setup_test_wrapper(): suite.get_hook("setup_test")(self.test) else: setup_test_wrapper = None if suite.has_hook("teardown_test"): def teardown_test_wrapper(): status_so_far = "passed" if is_location_successful(TreeLocation.in_test(self.test)) else "failed" suite.get_hook("teardown_test")(self.test, status_so_far) else: teardown_test_wrapper = None setup_teardown_funcs.append((setup_test_wrapper, teardown_test_wrapper)) scheduled_fixtures = context.fixture_registry.get_fixtures_scheduled_for_test( self.test, self.suite_scheduled_fixtures ) setup_teardown_funcs.extend(scheduled_fixtures.get_setup_teardown_pairs()) if any(setup for setup, _ in setup_teardown_funcs): context.event_manager.fire(events.TestSetupStartEvent(self.test)) set_step("Setup test") teardown_funcs = context.run_setup_funcs(setup_teardown_funcs, TreeLocation.in_test(self.test)) context.event_manager.fire(events.TestSetupEndEvent(self.test)) else: teardown_funcs = [teardown for _, teardown in setup_teardown_funcs if teardown] ### # Run test: ### if is_location_successful(TreeLocation.in_test(self.test)): test_func_params = scheduled_fixtures.get_fixture_results(self.test.get_fixtures()) set_step(self.test.description) try: self.test.callback(**test_func_params) except Exception as e: context.handle_exception(e, suite) ### # Teardown ### if any(teardown_funcs): context.event_manager.fire(events.TestTeardownStartEvent(self.test)) set_step("Teardown test") context.run_teardown_funcs(teardown_funcs) context.event_manager.fire(events.TestTeardownEndEvent(self.test)) context.event_manager.fire(events.TestEndEvent(self.test)) if not is_location_successful(TreeLocation.in_test(self.test)): raise TaskFailure("test '%s' failed" % self.test.path)
def skip(self, context, reason=""): context.event_manager.fire(events.TestSkippedEvent(self.test, "Test skipped because %s" % reason)) mark_location_as_failed(TreeLocation.in_test(self.test))