diff --git a/allure-pytest/src/listener.py b/allure-pytest/src/listener.py index 21c06750..95a85d4a 100644 --- a/allure-pytest/src/listener.py +++ b/allure-pytest/src/listener.py @@ -242,6 +242,28 @@ def pytest_runtest_makereport(self, item, call): if report.capstderr: self.attach_data(report.capstderr, "stderr", AttachmentType.TEXT, None) + @pytest.hookimpl(hookwrapper=True) + def pytest_runtest_logreport(self, report): + yield + if hasattr(report, "context"): + item_uuid = self._cache.get(report.nodeid) + step_uuid = uuid4() + step = TestStepResult(name=report.context.msg or report.head_line, start=report.start) + if report.longrepr: + status_details = StatusDetails( + message=report.longrepr.reprcrash.message, + trace=report.longreprtext + ) + else: + status_details = None + self.allure_logger.start_step(item_uuid, step_uuid, step) + self.allure_logger.stop_step( + step_uuid, + stop=report.stop, + status=report.outcome, + statusDetails=status_details + ) + @pytest.hookimpl(hookwrapper=True) def pytest_runtest_logfinish(self, nodeid, location): yield diff --git a/tests/allure_pytest/acceptance/step/subtest_as_step_test.py b/tests/allure_pytest/acceptance/step/subtest_as_step_test.py new file mode 100644 index 00000000..fa11d061 --- /dev/null +++ b/tests/allure_pytest/acceptance/step/subtest_as_step_test.py @@ -0,0 +1,65 @@ +from hamcrest import assert_that +from tests.allure_pytest.pytest_runner import AllurePytestRunner + +from allure_commons_test.report import has_test_case +from allure_commons_test.result import has_step +from allure_commons_test.result import with_status +from allure_commons_test.result import with_message_contains +from allure_commons_test.result import has_status_details + + +def test_with_subtest(allure_pytest_runner: AllurePytestRunner): + """ + >>> import allure + >>> import pytest + >>> @pytest.mark.skipif("pytest.__version__[0] < '9'") + >>> def test_with_subtest(subtests): + ... with subtests.test(msg='Some failed subtest'): + ... assert False, 'Some error' + ... with allure.step('Next step'): + ... pass + """ + + allure_results = allure_pytest_runner.run_docstring() + + assert_that( + allure_results, + has_test_case( + "test_with_subtest", + has_step( + "Some failed subtest", + with_status("failed"), + has_status_details( + with_message_contains("Some error") + ) + ), + has_step( + "Next step", + with_status("passed") + ), + with_status("failed") + ) + ) + + +def test_with_subtest_without_msg(allure_pytest_runner: AllurePytestRunner): + """ + >>> import allure + >>> import pytest + >>> @pytest.mark.skipif("pytest.__version__[0] < '9'") + >>> def test_with_subtest_without_msg(subtests): + ... with subtests.test(): + ... pass + """ + + allure_results = allure_pytest_runner.run_docstring() + + assert_that( + allure_results, + has_test_case( + "test_with_subtest_without_msg", + has_step( + "test_with_subtest_without_msg ()", + ), + ) + )