diff options
author | Bruno Oliveira <bruno@esss.co> | 2019-11-19 12:42:11 -0300 |
---|---|---|
committer | Bruno Oliveira <bruno@esss.co> | 2019-11-19 12:42:11 -0300 |
commit | 21622d0df43609b84644134a4741343d147df1eb (patch) | |
tree | e65f07b3d75209a5ad0548330c69cb0e145c498a /testing | |
parent | d1e2d12b3feae7366b57b878f88a8b772817820e (diff) | |
parent | f36ea240fe3579f945bf5d6cc41b5e45a572249d (diff) | |
download | pytest-21622d0df43609b84644134a4741343d147df1eb.tar.gz |
Merge remote-tracking branch 'upstream/master' into release-5.3.0
Diffstat (limited to 'testing')
-rw-r--r-- | testing/acceptance_test.py | 1 | ||||
-rw-r--r-- | testing/test_collection.py | 11 | ||||
-rw-r--r-- | testing/test_setupplan.py | 91 |
3 files changed, 100 insertions, 3 deletions
diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 578ab45eb..8f7be14be 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -760,7 +760,6 @@ class TestInvocationVariants: result = testdir.runpytest(str(p) + "::test", "--doctest-modules") result.stdout.fnmatch_lines(["*1 passed*"]) - @pytest.mark.skipif(not hasattr(os, "symlink"), reason="requires symlinks") def test_cmdline_python_package_symlink(self, testdir, monkeypatch): """ test --pyargs option with packages with path containing symlink can diff --git a/testing/test_collection.py b/testing/test_collection.py index 303738d67..b791ac6f9 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -685,6 +685,8 @@ class Test_genitems: def test_example_items1(self, testdir): p = testdir.makepyfile( """ + import pytest + def testone(): pass @@ -693,19 +695,24 @@ class Test_genitems: pass class TestY(TestX): - pass + @pytest.mark.parametrize("arg0", [".["]) + def testmethod_two(self, arg0): + pass """ ) items, reprec = testdir.inline_genitems(p) - assert len(items) == 3 + assert len(items) == 4 assert items[0].name == "testone" assert items[1].name == "testmethod_one" assert items[2].name == "testmethod_one" + assert items[3].name == "testmethod_two[.[]" # let's also test getmodpath here assert items[0].getmodpath() == "testone" assert items[1].getmodpath() == "TestX.testmethod_one" assert items[2].getmodpath() == "TestY.testmethod_one" + # PR #6202: Fix incorrect result of getmodpath method. (Resolves issue #6189) + assert items[3].getmodpath() == "TestY.testmethod_two[.[]" s = items[0].getmodpath(stopatmodule=False) assert s.endswith("test_example_items1.testone") diff --git a/testing/test_setupplan.py b/testing/test_setupplan.py index e323ba240..a44474dd1 100644 --- a/testing/test_setupplan.py +++ b/testing/test_setupplan.py @@ -17,3 +17,94 @@ def test_show_fixtures_and_test(testdir, dummy_yaml_custom_test): result.stdout.fnmatch_lines( ["*SETUP F arg*", "*test_arg (fixtures used: arg)", "*TEARDOWN F arg*"] ) + + +def test_show_multi_test_fixture_setup_and_teardown_correctly_simple(testdir): + """ + Verify that when a fixture lives for longer than a single test, --setup-plan + correctly displays the SETUP/TEARDOWN indicators the right number of times. + + As reported in https://github.com/pytest-dev/pytest/issues/2049 + --setup-plan was showing SETUP/TEARDOWN on every test, even when the fixture + should persist through multiple tests. + + (Note that this bug never affected actual test execution, which used the + correct fixture lifetimes. It was purely a display bug for --setup-plan, and + did not affect the related --setup-show or --setup-only.) + """ + testdir.makepyfile( + """ + import pytest + @pytest.fixture(scope = 'class') + def fix(): + return object() + class TestClass: + def test_one(self, fix): + assert False + def test_two(self, fix): + assert False + """ + ) + + result = testdir.runpytest("--setup-plan") + assert result.ret == 0 + + setup_fragment = "SETUP C fix" + setup_count = 0 + + teardown_fragment = "TEARDOWN C fix" + teardown_count = 0 + + for line in result.stdout.lines: + if setup_fragment in line: + setup_count += 1 + if teardown_fragment in line: + teardown_count += 1 + + # before the fix this tests, there would have been a setup/teardown + # message for each test, so the counts would each have been 2 + assert setup_count == 1 + assert teardown_count == 1 + + +def test_show_multi_test_fixture_setup_and_teardown_same_as_setup_show(testdir): + """ + Verify that SETUP/TEARDOWN messages match what comes out of --setup-show. + """ + testdir.makepyfile( + """ + import pytest + @pytest.fixture(scope = 'session') + def sess(): + return True + @pytest.fixture(scope = 'module') + def mod(): + return True + @pytest.fixture(scope = 'class') + def cls(): + return True + @pytest.fixture(scope = 'function') + def func(): + return True + def test_outside(sess, mod, cls, func): + assert True + class TestCls: + def test_one(self, sess, mod, cls, func): + assert True + def test_two(self, sess, mod, cls, func): + assert True + """ + ) + + plan_result = testdir.runpytest("--setup-plan") + show_result = testdir.runpytest("--setup-show") + + # the number and text of these lines should be identical + plan_lines = [ + l for l in plan_result.stdout.lines if "SETUP" in l or "TEARDOWN" in l + ] + show_lines = [ + l for l in show_result.stdout.lines if "SETUP" in l or "TEARDOWN" in l + ] + + assert plan_lines == show_lines |