From d488f899d8f773970a497b523088c7a024c1983a Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 7 Mar 2021 15:38:11 +0100 Subject: [PATCH 1/3] WIP: initial repro for #8412 --- testing/example_scripts/order_issue.py | 45 ++++++++++++++++++++++++++ testing/examples/test_order_issue.py | 7 ++++ 2 files changed, 52 insertions(+) create mode 100644 testing/example_scripts/order_issue.py create mode 100644 testing/examples/test_order_issue.py diff --git a/testing/example_scripts/order_issue.py b/testing/example_scripts/order_issue.py new file mode 100644 index 000000000..6de221fcc --- /dev/null +++ b/testing/example_scripts/order_issue.py @@ -0,0 +1,45 @@ +import os +import unittest + +import pytest + + +class EnvironmentAwareMixin: + @pytest.fixture(autouse=True) + def _monkeypatch(self, monkeypatch): + self._envpatcher = monkeypatch + + def set_environ(self, name, value): + self._envpatcher.setenv(name, value) + + +# This arrangement fails: setup_method runs before _monkeypatch +class MyPytestBase( + EnvironmentAwareMixin, +): + pass + + +class TestSomething(MyPytestBase): + @pytest.fixture(autouse=True) + def setup_method(self): + self.set_environ("X", "1") + + def test_something(self): + assert os.environ["X"] == "1" + + +# This arrangement works: _monkeypatch runs before setUp +class MyUnittestBase( + EnvironmentAwareMixin, + unittest.TestCase, +): + pass + + +class TestSomethingElse(MyUnittestBase): + def setUp(self): + self.set_environ("X", "1") + + def test_something_else(self): + assert os.environ["X"] == "1" diff --git a/testing/examples/test_order_issue.py b/testing/examples/test_order_issue.py new file mode 100644 index 000000000..4b3bfa8e3 --- /dev/null +++ b/testing/examples/test_order_issue.py @@ -0,0 +1,7 @@ +from _pytest.pytester import Pytester + + +def test_order(pytester: Pytester) -> None: + pytester.copy_example("order_issue.py") + rep = pytester.runpytest("order_issue.py") + rep.assert_outcomes(passed=2) From 2385d6f9cb52ee0e69ff4d248e50229b25be3a8d Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 7 Mar 2021 15:41:28 +0100 Subject: [PATCH 2/3] update example to @nedbat's current version --- testing/example_scripts/order_issue.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/testing/example_scripts/order_issue.py b/testing/example_scripts/order_issue.py index 6de221fcc..f5cca3491 100644 --- a/testing/example_scripts/order_issue.py +++ b/testing/example_scripts/order_issue.py @@ -13,15 +13,21 @@ class EnvironmentAwareMixin: self._envpatcher.setenv(name, value) -# This arrangement fails: setup_method runs before _monkeypatch +# This arrangement works: _monkeypatch does run class MyPytestBase( EnvironmentAwareMixin, ): pass +class TestAnotherThing(MyPytestBase): + def test_another_thing(self): + self.set_environ("X", "1") + assert os.environ["X"] == "1" + + +# This arrangement fails: setup_method runs before _monkeypatch class TestSomething(MyPytestBase): - @pytest.fixture(autouse=True) def setup_method(self): self.set_environ("X", "1") From 858a785dc6cc918fb03e9c70ba611e57016d8a4a Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 7 Mar 2021 15:53:47 +0100 Subject: [PATCH 3/3] use setup-show and extend reproducer with fixture/fixture+autouse checks --- testing/example_scripts/order_issue.py | 18 ++++++++++++++++++ testing/examples/test_order_issue.py | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/testing/example_scripts/order_issue.py b/testing/example_scripts/order_issue.py index f5cca3491..855501997 100644 --- a/testing/example_scripts/order_issue.py +++ b/testing/example_scripts/order_issue.py @@ -35,6 +35,24 @@ class TestSomething(MyPytestBase): assert os.environ["X"] == "1" +class TestSomethingWithFixture(MyPytestBase): + @pytest.fixture + def setup_method(self): + self.set_environ("X", "1") + + def test_something(self): + assert os.environ["X"] == "1" + + +class TestSomethingWithFixtureAutouse(MyPytestBase): + @pytest.fixture(autouse=True) + def setup_method(self): + self.set_environ("X", "1") + + def test_something(self): + assert os.environ["X"] == "1" + + # This arrangement works: _monkeypatch runs before setUp class MyUnittestBase( EnvironmentAwareMixin, diff --git a/testing/examples/test_order_issue.py b/testing/examples/test_order_issue.py index 4b3bfa8e3..a32609f95 100644 --- a/testing/examples/test_order_issue.py +++ b/testing/examples/test_order_issue.py @@ -3,5 +3,5 @@ from _pytest.pytester import Pytester def test_order(pytester: Pytester) -> None: pytester.copy_example("order_issue.py") - rep = pytester.runpytest("order_issue.py") + rep = pytester.runpytest("order_issue.py", "--setup-show") rep.assert_outcomes(passed=2)