|
| 1 | +import pytest |
| 2 | +from hamcrest import assert_that, empty |
| 3 | +from hamcrest import all_of, is_, is_not |
| 4 | +from hamcrest import has_property, has_value |
| 5 | +from hamcrest import contains_string |
| 6 | +from tests.allure_pytest.pytest_runner import AllurePytestRunner |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize("capture", ["sys", "fd", "no"]) |
| 10 | +def test_capture_stdout_in_bdd(allure_pytest_bdd_runner: AllurePytestRunner, capture): |
| 11 | + feature_content = ( |
| 12 | + """ |
| 13 | + Feature: Basic allure-pytest-bdd usage |
| 14 | + Scenario: Simple passed example |
| 15 | + Given the preconditions are satisfied |
| 16 | + When the action is invoked |
| 17 | + Then the postconditions are held |
| 18 | + """ |
| 19 | + ) |
| 20 | + steps_content = ( |
| 21 | + """ |
| 22 | + from pytest_bdd import scenario, given, when, then |
| 23 | + @scenario("scenario.feature", "Simple passed example") |
| 24 | + def test_scenario_passes(): |
| 25 | + pass |
| 26 | +
|
| 27 | + @given("the preconditions are satisfied") |
| 28 | + def given_the_preconditions_are_satisfied(): |
| 29 | + print("Print from given step") |
| 30 | +
|
| 31 | + @when("the action is invoked") |
| 32 | + def when_the_action_is_invoked(): |
| 33 | + print("Print from when step") |
| 34 | +
|
| 35 | + @then("the postconditions are held") |
| 36 | + def then_the_postconditions_are_held(): |
| 37 | + print("Print from then step") |
| 38 | + """ |
| 39 | + ) |
| 40 | + |
| 41 | + allure_results = allure_pytest_bdd_runner.run_pytest( |
| 42 | + ("scenario.feature", feature_content), |
| 43 | + steps_content, cli_args=(f"--capture={capture}",) |
| 44 | + ) |
| 45 | + if_pytest_capture_ = is_not if capture == "no" else is_ |
| 46 | + |
| 47 | + assert_that( |
| 48 | + allure_results, |
| 49 | + has_property( |
| 50 | + "attachments", |
| 51 | + all_of( |
| 52 | + if_pytest_capture_(has_value(contains_string("Print from given step"))), |
| 53 | + if_pytest_capture_(has_value(contains_string("Print from when step"))), |
| 54 | + if_pytest_capture_(has_value(contains_string("Print from then step"))) |
| 55 | + ) |
| 56 | + ) |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize("capture", ["sys", "fd"]) |
| 61 | +def test_capture_empty_stdout(allure_pytest_bdd_runner: AllurePytestRunner, capture): |
| 62 | + feature_content = ( |
| 63 | + """ |
| 64 | + Feature: Basic allure-pytest-bdd usage |
| 65 | + Scenario: Simple passed example |
| 66 | + Given the preconditions are satisfied |
| 67 | + When the action is invoked |
| 68 | + Then the postconditions are held |
| 69 | + """ |
| 70 | + ) |
| 71 | + steps_content = ( |
| 72 | + """ |
| 73 | + from pytest_bdd import scenario, given, when, then |
| 74 | + @scenario("scenario.feature", "Simple passed example") |
| 75 | + def test_scenario_passes(): |
| 76 | + pass |
| 77 | +
|
| 78 | + @given("the preconditions are satisfied") |
| 79 | + def given_the_preconditions_are_satisfied(): |
| 80 | + pass |
| 81 | +
|
| 82 | + @when("the action is invoked") |
| 83 | + def when_the_action_is_invoked(): |
| 84 | + pass |
| 85 | +
|
| 86 | + @then("the postconditions are held") |
| 87 | + def then_the_postconditions_are_held(): |
| 88 | + pass |
| 89 | + """ |
| 90 | + ) |
| 91 | + |
| 92 | + allure_results = allure_pytest_bdd_runner.run_pytest( |
| 93 | + ("scenario.feature", feature_content), |
| 94 | + steps_content, cli_args=(f"--capture={capture}",) |
| 95 | + ) |
| 96 | + |
| 97 | + assert_that( |
| 98 | + allure_results, |
| 99 | + has_property("attachments", empty()) |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("logging", [True, False]) |
| 104 | +def test_capture_log(allure_pytest_bdd_runner: AllurePytestRunner, logging): |
| 105 | + feature_content = ( |
| 106 | + """ |
| 107 | + Feature: Basic allure-pytest-bdd usage |
| 108 | + Scenario: Simple passed example |
| 109 | + Given the preconditions are satisfied |
| 110 | + When the action is invoked |
| 111 | + Then the postconditions are held |
| 112 | + """ |
| 113 | + ) |
| 114 | + steps_content = ( |
| 115 | + """ |
| 116 | + import logging |
| 117 | + from pytest_bdd import scenario, given, when, then |
| 118 | + logger = logging.getLogger(__name__) |
| 119 | + @scenario("scenario.feature", "Simple passed example") |
| 120 | + def test_scenario_passes(): |
| 121 | + pass |
| 122 | +
|
| 123 | + @given("the preconditions are satisfied") |
| 124 | + def given_the_preconditions_are_satisfied(): |
| 125 | + logging.info("Logging from given step") |
| 126 | +
|
| 127 | + @when("the action is invoked") |
| 128 | + def when_the_action_is_invoked(): |
| 129 | + logging.info("Logging from when step") |
| 130 | +
|
| 131 | + @then("the postconditions are held") |
| 132 | + def then_the_postconditions_are_held(): |
| 133 | + logging.info("Logging from then step") |
| 134 | + """ |
| 135 | + ) |
| 136 | + |
| 137 | + params = [] if logging else ["-p", "no:logging"] |
| 138 | + allure_results = allure_pytest_bdd_runner.run_pytest( |
| 139 | + ("scenario.feature", feature_content), |
| 140 | + steps_content, cli_args=("--log-level=INFO", *params) |
| 141 | + ) |
| 142 | + |
| 143 | + if_logging_ = is_ if logging else is_not |
| 144 | + |
| 145 | + assert_that( |
| 146 | + allure_results, |
| 147 | + has_property( |
| 148 | + "attachments", |
| 149 | + all_of( |
| 150 | + if_logging_(has_value(contains_string("Logging from given step"))), |
| 151 | + if_logging_(has_value(contains_string("Logging from when step"))), |
| 152 | + if_logging_(has_value(contains_string("Logging from then step"))), |
| 153 | + ) |
| 154 | + ) |
| 155 | + ) |
0 commit comments