From c8677a7783c4373a0ff21be8aea5eb899d16e1a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20R=C3=B6thlisberger?= Date: Fri, 4 Nov 2022 08:26:09 +0000 Subject: [PATCH] tests: Use `pytester` fixture instead of `testdir` `testdir` is deprecated. It's identical to `pytester`, except that `testdir` returns `py.path` objects whereas `pytester` returns Python stdlib `Path` objects. So I had to update usages of those paths. `pytester` was added in pytest 6.2, which is our minimum supported version. --- src/pytest_bdd/utils.py | 2 +- tests/args/cfparse/test_args.py | 16 ++-- tests/args/parse/test_args.py | 16 ++-- tests/args/regex/test_args.py | 24 ++--- tests/args/test_common.py | 18 ++-- tests/feature/test_alias.py | 8 +- tests/feature/test_background.py | 20 ++-- tests/feature/test_cucumber_json.py | 18 ++-- tests/feature/test_description.py | 8 +- tests/feature/test_feature_base_dir.py | 34 +++---- .../feature/test_gherkin_terminal_reporter.py | 66 ++++++------- tests/feature/test_multiline.py | 16 ++-- tests/feature/test_no_scenario.py | 13 ++- tests/feature/test_no_sctrict_gherkin.py | 16 ++-- tests/feature/test_outline.py | 38 ++++---- tests/feature/test_outline_empty_values.py | 10 +- tests/feature/test_report.py | 36 +++---- tests/feature/test_same_function_name.py | 8 +- tests/feature/test_scenario.py | 40 ++++---- tests/feature/test_scenarios.py | 26 ++--- tests/feature/test_steps.py | 96 +++++++++---------- tests/feature/test_tags.py | 56 +++++------ tests/feature/test_wrong.py | 8 +- tests/generation/test_generate_missing.py | 16 ++-- tests/library/test_parent.py | 68 ++++++------- tests/scripts/test_generate.py | 21 ++-- tests/scripts/test_migrate.py | 12 +-- tests/steps/test_common.py | 32 +++---- tests/steps/test_given.py | 8 +- tests/steps/test_unicode.py | 16 ++-- tests/test_hooks.py | 22 ++--- 31 files changed, 393 insertions(+), 395 deletions(-) diff --git a/src/pytest_bdd/utils.py b/src/pytest_bdd/utils.py index 2fdeee6..3554078 100644 --- a/src/pytest_bdd/utils.py +++ b/src/pytest_bdd/utils.py @@ -68,7 +68,7 @@ def collect_dumped_objects(result: RunResult) -> list: """Parse all the objects dumped with `dump_object` from the result. Note: You must run the result with output to stdout enabled. - For example, using ``testdir.runpytest("-s")``. + For example, using ``pytester.runpytest("-s")``. """ stdout = result.stdout.str() # pytest < 6.2, otherwise we could just do str(result.stdout) payloads = re.findall(rf"{_DUMP_START}(.*?){_DUMP_END}", stdout) diff --git a/tests/args/cfparse/test_args.py b/tests/args/cfparse/test_args.py index c02daed..2a39a46 100644 --- a/tests/args/cfparse/test_args.py +++ b/tests/args/cfparse/test_args.py @@ -3,9 +3,9 @@ import textwrap -def test_every_step_takes_param_with_the_same_name(testdir): +def test_every_step_takes_param_with_the_same_name(pytester): """Test every step takes param with the same name.""" - testdir.makefile( + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -21,7 +21,7 @@ def test_every_step_takes_param_with_the_same_name(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """\ import pytest @@ -53,13 +53,13 @@ def test_every_step_takes_param_with_the_same_name(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) -def test_argument_in_when(testdir): +def test_argument_in_when(pytester): """Test step arguments in when steps.""" - testdir.makefile( + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -72,7 +72,7 @@ def test_argument_in_when(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """\ import pytest @@ -105,5 +105,5 @@ def test_argument_in_when(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) diff --git a/tests/args/parse/test_args.py b/tests/args/parse/test_args.py index 6ad368d..b889f17 100644 --- a/tests/args/parse/test_args.py +++ b/tests/args/parse/test_args.py @@ -3,8 +3,8 @@ import textwrap -def test_every_steps_takes_param_with_the_same_name(testdir): - testdir.makefile( +def test_every_steps_takes_param_with_the_same_name(pytester): + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -20,7 +20,7 @@ def test_every_steps_takes_param_with_the_same_name(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """\ import pytest @@ -52,12 +52,12 @@ def test_every_steps_takes_param_with_the_same_name(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) -def test_argument_in_when_step_1(testdir): - testdir.makefile( +def test_argument_in_when_step_1(pytester): + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -70,7 +70,7 @@ def test_argument_in_when_step_1(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """\ import pytest @@ -103,5 +103,5 @@ def test_argument_in_when_step_1(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) diff --git a/tests/args/regex/test_args.py b/tests/args/regex/test_args.py index 9b31e9c..cce2d59 100644 --- a/tests/args/regex/test_args.py +++ b/tests/args/regex/test_args.py @@ -3,8 +3,8 @@ import textwrap -def test_every_steps_takes_param_with_the_same_name(testdir): - testdir.makefile( +def test_every_steps_takes_param_with_the_same_name(pytester): + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -20,7 +20,7 @@ def test_every_steps_takes_param_with_the_same_name(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( r""" import pytest @@ -51,17 +51,17 @@ def test_every_steps_takes_param_with_the_same_name(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) -def test_exact_match(testdir): +def test_exact_match(pytester): """Test that parsers.re does an exact match (fullmatch) of the whole string. This tests exists because in the past we only used re.match, which only finds a match at the beginning of the string, so if there were any more characters not matching at the end, they were ignored""" - testdir.makefile( + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -75,7 +75,7 @@ def test_exact_match(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( r""" import pytest @@ -101,15 +101,15 @@ def test_exact_match(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(failed=1) result.stdout.fnmatch_lines( '*StepDefinitionNotFoundError: Step definition is not found: When "I pay 1 Euro by mistake"*' ) -def test_argument_in_when(testdir): - testdir.makefile( +def test_argument_in_when(pytester): + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -122,7 +122,7 @@ def test_argument_in_when(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( r""" import pytest @@ -155,5 +155,5 @@ def test_argument_in_when(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) diff --git a/tests/args/test_common.py b/tests/args/test_common.py index 5500ff2..9192e81 100644 --- a/tests/args/test_common.py +++ b/tests/args/test_common.py @@ -3,8 +3,8 @@ import textwrap from pytest_bdd.utils import collect_dumped_objects -def test_reuse_same_step_different_converters(testdir): - testdir.makefile( +def test_reuse_same_step_different_converters(pytester): + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -19,7 +19,7 @@ def test_reuse_same_step_different_converters(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( r""" import pytest @@ -43,7 +43,7 @@ def test_reuse_same_step_different_converters(testdir): """ ) ) - result = testdir.runpytest("-s") + result = pytester.runpytest("-s") result.assert_outcomes(passed=1) [int_value, str_value, float_value] = collect_dumped_objects(result) @@ -57,9 +57,9 @@ def test_reuse_same_step_different_converters(testdir): assert float_value == 42.0 -def test_string_steps_dont_take_precedence(testdir): +def test_string_steps_dont_take_precedence(pytester): """Test that normal steps don't take precedence over the other steps.""" - testdir.makefile( + pytester.makefile( ".feature", arguments=textwrap.dedent( """\ @@ -71,7 +71,7 @@ def test_string_steps_dont_take_precedence(testdir): """ ), ) - testdir.makeconftest( + pytester.makeconftest( textwrap.dedent( """ from pytest_bdd import given, when, then, parsers @@ -92,7 +92,7 @@ def test_string_steps_dont_take_precedence(testdir): ) ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( r""" import pytest @@ -109,7 +109,7 @@ def test_string_steps_dont_take_precedence(testdir): """ ) ) - result = testdir.runpytest("-s") + result = pytester.runpytest("-s") result.assert_outcomes(passed=1) [which] = collect_dumped_objects(result) diff --git a/tests/feature/test_alias.py b/tests/feature/test_alias.py index 6253e76..ce2b68c 100644 --- a/tests/feature/test_alias.py +++ b/tests/feature/test_alias.py @@ -3,8 +3,8 @@ import textwrap -def test_step_alias(testdir): - testdir.makefile( +def test_step_alias(pytester): + pytester.makefile( ".feature", alias=textwrap.dedent( """\ @@ -22,7 +22,7 @@ def test_step_alias(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """\ import pytest @@ -56,5 +56,5 @@ def test_step_alias(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) diff --git a/tests/feature/test_background.py b/tests/feature/test_background.py index 03285e6..be0490e 100644 --- a/tests/feature/test_background.py +++ b/tests/feature/test_background.py @@ -73,13 +73,13 @@ def _(foo): """ -def test_background_basic(testdir): +def test_background_basic(pytester): """Test feature background.""" - testdir.makefile(".feature", background=textwrap.dedent(FEATURE)) + pytester.makefile(".feature", background=textwrap.dedent(FEATURE)) - testdir.makeconftest(textwrap.dedent(STEPS)) + pytester.makeconftest(textwrap.dedent(STEPS)) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """\ from pytest_bdd import scenario @@ -91,18 +91,18 @@ def test_background_basic(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) -def test_background_check_order(testdir): +def test_background_check_order(pytester): """Test feature background to ensure that background steps are executed first.""" - testdir.makefile(".feature", background=textwrap.dedent(FEATURE)) + pytester.makefile(".feature", background=textwrap.dedent(FEATURE)) - testdir.makeconftest(textwrap.dedent(STEPS)) + pytester.makeconftest(textwrap.dedent(STEPS)) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """\ from pytest_bdd import scenario @@ -114,5 +114,5 @@ def test_background_check_order(testdir): """ ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=1) diff --git a/tests/feature/test_cucumber_json.py b/tests/feature/test_cucumber_json.py index 7d83a60..f14a100 100644 --- a/tests/feature/test_cucumber_json.py +++ b/tests/feature/test_cucumber_json.py @@ -10,10 +10,10 @@ if TYPE_CHECKING: from _pytest.pytester import RunResult, Testdir -def runandparse(testdir: Testdir, *args: Any) -> tuple[RunResult, list[dict[str, Any]]]: +def runandparse(pytester: Pytester, *args: Any) -> tuple[RunResult, list[dict[str, Any]]]: """Run tests in testdir and parse json output.""" - resultpath = testdir.tmpdir.join("cucumber.json") - result = testdir.runpytest(f"--cucumberjson={resultpath}", "-s", *args) + resultpath = pytester.path.joinpath("cucumber.json") + result = pytester.runpytest(f"--cucumberjson={resultpath}", "-s", *args) with resultpath.open() as f: jsonobject = json.load(f) return result, jsonobject @@ -29,9 +29,9 @@ class OfType: return isinstance(other, self.type) if self.type else True -def test_step_trace(testdir): +def test_step_trace(pytester): """Test step trace.""" - testdir.makefile( + pytester.makefile( ".ini", pytest=textwrap.dedent( """ @@ -44,7 +44,7 @@ def test_step_trace(testdir): """ ), ) - testdir.makefile( + pytester.makefile( ".feature", test=textwrap.dedent( """ @@ -73,7 +73,7 @@ def test_step_trace(testdir): """ ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """ import pytest @@ -109,7 +109,7 @@ def test_step_trace(testdir): """ ) ) - result, jsonobject = runandparse(testdir) + result, jsonobject = runandparse(pytester) result.assert_outcomes(passed=4, failed=1) assert result.ret @@ -227,7 +227,7 @@ def test_step_trace(testdir): "line": 2, "name": "One passing scenario, one failing scenario", "tags": [{"name": "feature-tag", "line": 1}], - "uri": os.path.join(testdir.tmpdir.basename, "test.feature"), + "uri": os.path.join(pytester.path.name, "test.feature"), } ] diff --git a/tests/feature/test_description.py b/tests/feature/test_description.py index 2366d59..9678bff 100644 --- a/tests/feature/test_description.py +++ b/tests/feature/test_description.py @@ -3,9 +3,9 @@ import textwrap -def test_description(testdir): +def test_description(pytester): """Test description for the feature.""" - testdir.makefile( + pytester.makefile( ".feature", description=textwrap.dedent( """\ @@ -24,7 +24,7 @@ def test_description(testdir): ), ) - testdir.makepyfile( + pytester.makepyfile( textwrap.dedent( """\ import textwrap @@ -53,5 +53,5 @@ def test_description(testdir): ) ) - result = testdir.runpytest() + result = pytester.runpytest() result.assert_outcomes(passed=2) diff --git a/tests/feature/test_feature_base_dir.py b/tests/feature/test_feature_base_dir.py index bbbe97a..a433b51 100644 --- a/tests/feature/test_feature_base_dir.py +++ b/tests/feature/test_feature_base_dir.py @@ -5,44 +5,44 @@ NOT_EXISTING_FEATURE_PATHS = [".", "/does/not/exist/"] @pytest.mark.parametrize("base_dir", NOT_EXISTING_FEATURE_PATHS) -def test_feature_path_not_found(testdir, base_dir): +def test_feature_path_not_found(pytester, base_dir): """Test feature base dir.""" - prepare_testdir(testdir, base_dir) + prepare_testdir(pytester, base_dir) - result = testdir.runpytest("-k", "test_not_found_by_ini") + result = pytester.runpytest("-k", "test_not_found_by_ini") result.assert_outcomes(passed=2) -def test_feature_path_ok(testdir): +def test_feature_path_ok(pytester): base_dir = "features" - prepare_testdir(testdir, base_dir) + prepare_testdir(pytester, base_dir) - result = testdir.runpytest("-k", "test_ok_by_ini") + result = pytester.runpytest("-k", "test_ok_by_ini") result.assert_outcomes(passed=2) -def test_feature_path_by_param_not_found(testdir): +def test_feature_path_by_param_not_found(pytester): """As param takes precedence even if ini config is correct it should fail if passed param is incorrect""" base_dir = "features" - prepare_testdir(testdir, base_dir) + prepare_testdir(pytester, base_dir) - result = testdir.runpytest("-k", "test_not_found_by_param") + result = pytester.runpytest("-k", "test_not_found_by_param") result.assert_outcomes(passed=4) @pytest.mark.parametrize("base_dir", NOT_EXISTING_FEATURE_PATHS) -def test_feature_path_by_param_ok(testdir, base_dir): +def test_feature_path_by_param_ok(pytester, base_dir): """If ini config is incorrect but param path is fine it should be able to find features""" - prepare_testdir(testdir, base_dir) + prepare_testdir(pytester, base_dir) - result = testdir.runpytest("-k", "test_ok_by_param") + result = pytester.runpytest("-k", "test_ok_by_param") result.assert_outcomes(passed=2) -def prepare_testdir(testdir, ini_base_dir): - testdir.makeini( +def prepare_testdir(pytester, ini_base_dir): + pytester.makeini( """ [pytest] bdd_features_base_dir={} @@ -51,8 +51,8 @@ def prepare_testdir(testdir, ini_base_dir): ) ) - feature_file = testdir.mkdir("features").join("steps.feature") - feature_file.write( + feature_file = pytester.mkdir("features").joinpath("steps.feature") + feature_file.write_text( """ Feature: Feature path Scenario: When scenario found @@ -60,7 +60,7 @@ def prepare_testdir(testdir, ini_base_dir): """ ) - testdir.makepyfile( + pytester.makepyfile( """ import os.path diff --git a/tests/feature/test_gherkin_terminal_reporter.py b/tests/feature/test_gherkin_terminal_reporter.py index f1bebb4..9ad78ba 100644 --- a/tests/feature/test_gherkin_terminal_reporter.py +++ b/tests/feature/test_gherkin_terminal_reporter.py @@ -37,11 +37,11 @@ def test_scenario_1(): """ -def test_default_output_should_be_the_same_as_regular_terminal_reporter(testdir): - testdir.makefile(".feature", test=FEATURE) - testdir.makepyfile(TEST) - regular = testdir.runpytest() - gherkin = testdir.runpytest("--gherkin-terminal-reporter") +def test_default_output_should_be_the_same_as_regular_terminal_reporter(pytester): + pytester.makefile(".feature", test=FEATURE) + pytester.makepyfile(TEST) + regular = pytester.runpytest() + gherkin = pytester.runpytest("--gherkin-terminal-reporter") regular.assert_outcomes(passed=1, failed=0) gherkin.assert_outcomes(passed=1, failed=0) @@ -51,17 +51,17 @@ def test_default_output_should_be_the_same_as_regular_terminal_reporter(testdir) assert all(l1 == l2 for l1, l2 in zip(parse_lines(regular.stdout.lines), parse_lines(gherkin.stdout.lines))) -def test_verbose_mode_should_display_feature_and_scenario_names_instead_of_test_names_in_a_single_line(testdir): - testdir.makefile(".feature", test=FEATURE) - testdir.makepyfile(TEST) - result = testdir.runpytest("--gherkin-terminal-reporter", "-v") +def test_verbose_mode_should_display_feature_and_scenario_names_instead_of_test_names_in_a_single_line(pytester): + pytester.makefile(".feature", test=FEATURE) + pytester.makepyfile(TEST) + result = pytester.runpytest("--gherkin-terminal-reporter", "-v") result.assert_outcomes(passed=1, failed=0) result.stdout.fnmatch_lines("Feature: Gherkin terminal output feature") result.stdout.fnmatch_lines("*Scenario: Scenario example 1 PASSED") -def test_verbose_mode_should_preserve_displaying_regular_tests_as_usual(testdir): - testdir.makepyfile( +def test_verbose_mode_should_preserve_displaying_regular_tests_as_usual(pytester): + pytester.makepyfile( textwrap.dedent( """\ def test_1(): @@ -69,8 +69,8 @@ def test_verbose_mode_should_preserve_displaying_regular_tests_as_usual(testdir) """ ) ) - regular = testdir.runpytest() - gherkin = testdir.runpytest("--gherkin-terminal-reporter", "-v") + regular = pytester.runpytest() + gherkin = pytester.runpytest("--gherkin-terminal-reporter", "-v") regular.assert_outcomes(passed=1, failed=0) gherkin.assert_outcomes(passed=1, failed=0) @@ -80,10 +80,10 @@ def test_verbose_mode_should_preserve_displaying_regular_tests_as_usual(testdir) ) -def test_double_verbose_mode_should_display_full_scenario_description(testdir): - testdir.makefile(".feature", test=FEATURE) - testdir.makepyfile(TEST) - result = testdir.runpytest("--gherkin-terminal-reporter", "-vv") +def test_double_verbose_mode_should_display_full_scenario_description(pytester): + pytester.makefile(".feature", test=FEATURE) + pytester.makepyfile(TEST) + result = pytester.runpytest("--gherkin-terminal-reporter", "-vv") result.assert_outcomes(passed=1, failed=0) result.stdout.fnmatch_lines("*Scenario: Scenario example 1") @@ -94,9 +94,9 @@ def test_double_verbose_mode_should_display_full_scenario_description(testdir): @pytest.mark.parametrize("verbosity", ["", "-v", "-vv"]) -def test_error_message_for_missing_steps(testdir, verbosity): - testdir.makefile(".feature", test=FEATURE) - testdir.makepyfile( +def test_error_message_for_missing_steps(pytester, verbosity): + pytester.makefile(".feature", test=FEATURE) + pytester.makepyfile( textwrap.dedent( """\ from pytest_bdd import scenarios @@ -105,7 +105,7 @@ def test_error_message_for_missing_steps(testdir, verbosity): """ ) ) - result = testdir.runpytest("--gherkin-terminal-reporter", verbosity) + result = pytester.runpytest("--gherkin-terminal-reporter", verbosity) result.assert_outcomes(passed=0, failed=1) result.stdout.fnmatch_lines( """*StepDefinitionNotFoundError: Step definition is not found: Given "there is a bar". """ @@ -114,9 +114,9 @@ def test_error_message_for_missing_steps(testdir, verbosity): @pytest.mark.parametrize("verbosity", ["", "-v", "-vv"]) -def test_error_message_should_be_displayed(testdir, verbosity): - testdir.makefile(".feature", test=FEATURE) - testdir.makepyfile( +def test_error_message_should_be_displayed(pytester, verbosity): + pytester.makefile(".feature", test=FEATURE) + pytester.makepyfile( textwrap.dedent( """\ from pytest_bdd import given, when, then, scenario @@ -142,15 +142,15 @@ def test_error_message_should_be_displayed(testdir, verbosity): """ ) ) - result = testdir.runpytest("--gherkin-terminal-reporter", verbosity) + result = pytester.runpytest("--gherkin-terminal-reporter", verbosity) result.assert_outcomes(passed=0, failed=1) result.stdout.fnmatch_lines("E Exception: BIGBADABOOM") result.stdout.fnmatch_lines("test_error_message_should_be_displayed.py:15: Exception") -def test_local_variables_should_be_displayed_when_showlocals_option_is_used(testdir): - testdir.makefile(".feature", test=FEATURE) - testdir.makepyfile( +def test_local_variables_should_be_displayed_when_showlocals_option_is_used(pytester): + pytester.makefile(".feature", test=FEATURE) + pytester.makepyfile( textwrap.dedent( """\ from pytest_bdd import given, when, then, scenario @@ -177,15 +177,15 @@ def test_local_variables_should_be_displayed_when_showlocals_option_is_used(test """ ) ) - result = testdir.runpytest("--gherkin-terminal-reporter", "--showlocals") + result = pytester.runpytest("--gherkin-terminal-reporter", "--showlocals") result.assert_outcomes(passed=0, failed=1) result.stdout.fnmatch_lines("""request*=*