From 5b9d37c9573cdc2fcbbd95eaa83d3ea10b8a6dd1 Mon Sep 17 00:00:00 2001 From: Aleksandr Brodin <40681323+ShurikMen@users.noreply.github.com> Date: Thu, 7 Dec 2023 18:28:00 +0700 Subject: [PATCH] Ignore --clean-alluredir when using --collectonly (#753) --- .gitignore | 1 + allure-pytest-bdd/src/plugin.py | 2 +- allure-pytest/src/plugin.py | 2 +- .../acceptance/results/__init__.py | 0 .../acceptance/results/results_test.py | 57 ++++++++++++ .../acceptance/results/__init__.py | 0 .../acceptance/results/results_test.py | 90 +++++++++++++++++++ 7 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 tests/allure_pytest/acceptance/results/__init__.py create mode 100644 tests/allure_pytest/acceptance/results/results_test.py create mode 100644 tests/allure_pytest_bdd/acceptance/results/__init__.py create mode 100644 tests/allure_pytest_bdd/acceptance/results/results_test.py diff --git a/.gitignore b/.gitignore index 523fc340..25e4deb0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .tox .pytest_cache .python-version +.venv *.pyc *.egg-info diff --git a/allure-pytest-bdd/src/plugin.py b/allure-pytest-bdd/src/plugin.py index a5620264..5d6b8310 100644 --- a/allure-pytest-bdd/src/plugin.py +++ b/allure-pytest-bdd/src/plugin.py @@ -27,7 +27,7 @@ def clean_up(): def pytest_configure(config): report_dir = config.option.allure_report_dir - clean = config.option.clean_alluredir + clean = False if config.option.collectonly else config.option.clean_alluredir if report_dir: report_dir = os.path.abspath(report_dir) diff --git a/allure-pytest/src/plugin.py b/allure-pytest/src/plugin.py index 52a8eaff..2771722f 100644 --- a/allure-pytest/src/plugin.py +++ b/allure-pytest/src/plugin.py @@ -151,7 +151,7 @@ def pytest_addhooks(pluginmanager): def pytest_configure(config): report_dir = config.option.allure_report_dir - clean = config.option.clean_alluredir + clean = False if config.option.collectonly else config.option.clean_alluredir test_helper = AllureTestHelper(config) allure_commons.plugin_manager.register(test_helper) diff --git a/tests/allure_pytest/acceptance/results/__init__.py b/tests/allure_pytest/acceptance/results/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/allure_pytest/acceptance/results/results_test.py b/tests/allure_pytest/acceptance/results/results_test.py new file mode 100644 index 00000000..39c996c3 --- /dev/null +++ b/tests/allure_pytest/acceptance/results/results_test.py @@ -0,0 +1,57 @@ +from allure_commons_test.report import AllureReport + +from tests.allure_pytest.pytest_runner import AllurePytestRunner + + +TEST_FUNC = "def test_first_func(): pass" + + +def test_custom_alluredir(allure_pytest_runner: AllurePytestRunner): + alluredir = allure_pytest_runner.pytester.path + allure_pytest_runner.in_memory = False + + # run test twice + # results of all runs must be in the results directory + for _ in range(2): + allure_pytest_runner.run_pytest( + TEST_FUNC, + cli_args=["--alluredir", "allure_results"] + ) + assert (alluredir / 'allure_results').exists() + results = AllureReport(alluredir / 'allure_results') + assert len(results.test_cases) == 2 + + +def test_clean_alluredir(allure_pytest_runner: AllurePytestRunner): + alluredir = allure_pytest_runner.pytester.path + allure_pytest_runner.in_memory = False + + # run test twice + # results of only last runs must be in the results directory + for _ in range(2): + allure_pytest_runner.run_pytest( + TEST_FUNC, + cli_args=["--alluredir", "allure_results", "--clean-alluredir"] + ) + results = AllureReport(alluredir / 'allure_results') + assert len(results.test_cases) == 1 + + +def test_clean_alluredir_with_collectonly(allure_pytest_runner: AllurePytestRunner): + alluredir = allure_pytest_runner.pytester.path + allure_pytest_runner.in_memory = False + + # run test + allure_pytest_runner.run_pytest( + TEST_FUNC, + cli_args=["--alluredir", "allure_results"] + ) + results_before_clean = AllureReport(alluredir / 'allure_results') + # run test with --collectonly + allure_pytest_runner.run_pytest( + TEST_FUNC, + cli_args=["--alluredir", "allure_results", "--clean-alluredir", "--collectonly"] + ) + # results should be the same + results_after_clean = AllureReport(alluredir / 'allure_results') + assert results_before_clean.test_cases == results_after_clean.test_cases diff --git a/tests/allure_pytest_bdd/acceptance/results/__init__.py b/tests/allure_pytest_bdd/acceptance/results/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/allure_pytest_bdd/acceptance/results/results_test.py b/tests/allure_pytest_bdd/acceptance/results/results_test.py new file mode 100644 index 00000000..f8f35d2c --- /dev/null +++ b/tests/allure_pytest_bdd/acceptance/results/results_test.py @@ -0,0 +1,90 @@ +from allure_commons_test.report import AllureReport + +from tests.allure_pytest.pytest_runner import AllurePytestRunner + + +FEATURE_CONTENT = ( + """ + Feature: Basic allure-pytest-bdd usage + Scenario: Simple passed example + Given the preconditions are satisfied + When the action is invoked + Then the postconditions are held + """ +) +STEPS_CONTENT = ( + """ + from pytest_bdd import scenario, given, when, then + + @scenario("scenario.feature", "Simple passed example") + def test_scenario_passes(): + pass + + @given("the preconditions are satisfied") + def given_the_preconditions_are_satisfied(): + pass + + @when("the action is invoked") + def when_the_action_is_invoked(): + pass + + @then("the postconditions are held") + def then_the_postconditions_are_held(): + pass + """ +) + + +def test_custom_alluredir(allure_pytest_bdd_runner: AllurePytestRunner): + alluredir = allure_pytest_bdd_runner.pytester.path + allure_pytest_bdd_runner.in_memory = False + + # run test twice + # results of all runs must be in the results directory + for _ in range(2): + allure_pytest_bdd_runner.run_pytest( + ("scenario.feature", FEATURE_CONTENT), + STEPS_CONTENT, + cli_args=["--alluredir", "allure_results"] + ) + assert (alluredir / 'allure_results').exists() + results = AllureReport(alluredir / 'allure_results') + assert len(results.test_cases) == 2 + + +def test_clean_alluredir(allure_pytest_bdd_runner: AllurePytestRunner): + alluredir = allure_pytest_bdd_runner.pytester.path + allure_pytest_bdd_runner.in_memory = False + + # run test twice + # results of only last runs must be in the results directory + for _ in range(2): + allure_pytest_bdd_runner.run_pytest( + ("scenario.feature", FEATURE_CONTENT), + STEPS_CONTENT, + cli_args=["--alluredir", "allure_results", "--clean-alluredir"] + ) + results = AllureReport(alluredir / 'allure_results') + assert len(results.test_cases) == 1 + + +def test_clean_alluredir_with_collectonly(allure_pytest_bdd_runner: AllurePytestRunner): + alluredir = allure_pytest_bdd_runner.pytester.path + allure_pytest_bdd_runner.in_memory = False + + # run test + allure_pytest_bdd_runner.run_pytest( + ("scenario.feature", FEATURE_CONTENT), + STEPS_CONTENT, + cli_args=["--alluredir", "allure_results"] + ) + results_before_clean = AllureReport(alluredir / 'allure_results') + # run test with --collectonly + allure_pytest_bdd_runner.run_pytest( + ("scenario.feature", FEATURE_CONTENT), + STEPS_CONTENT, + cli_args=["--alluredir", "allure_results", "--clean-alluredir", "--collectonly"] + ) + # results should be the same + results_after_clean = AllureReport(alluredir / 'allure_results') + assert results_before_clean.test_cases == results_after_clean.test_cases