From 35b3b1097ffc576b11cd8ad205103666a90e2344 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 30 Aug 2019 10:54:07 -0300 Subject: [PATCH] Improve CHANGELOG and make test easier to understand for #570 --- changelog/570.bugfix.rst | 3 ++- testing/python/fixtures.py | 46 +++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/changelog/570.bugfix.rst b/changelog/570.bugfix.rst index 8936ff96abd..980491e281e 100644 --- a/changelog/570.bugfix.rst +++ b/changelog/570.bugfix.rst @@ -1 +1,2 @@ -Fix the scope behavior with indirect fixtures. +Fixed long standing issue where fixture scope was not respected when indirect fixtures were used during +parametrization. diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index a129aa58296..d998e97265f 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -4012,43 +4012,53 @@ def test_fixture_named_request(testdir): ) -def test_indirect_fixture(testdir): +def test_indirect_fixture_does_not_break_scope(testdir): + """Ensure that fixture scope is respected when using indirect fixtures (#570)""" testdir.makepyfile( """ - from collections import Counter - import pytest + instantiated = [] @pytest.fixture(scope="session") - def fixture_1(request, count=Counter()): - count[request.param] += 1 - yield count[request.param] + def fixture_1(request): + instantiated.append(("fixture_1", request.param)) @pytest.fixture(scope="session") def fixture_2(request): - yield request.param + instantiated.append(("fixture_2", request.param)) scenarios = [ - ("a", "a1"), - ("a", "a2"), - ("b", "b1"), - ("b", "b2"), - ("c", "c1"), - ("c", "c2"), + ("A", "a1"), + ("A", "a2"), + ("B", "b1"), + ("B", "b2"), + ("C", "c1"), + ("C", "c2"), ] - @pytest.mark.parametrize( "fixture_1,fixture_2", scenarios, indirect=["fixture_1", "fixture_2"] ) - def test_it(fixture_1, fixture_2): - assert fixture_1 == 1 - assert fixture_2[1] in ("1", "2") + def test_create_fixtures(fixture_1, fixture_2): + pass + + def test_check_fixture_instantiations(): + assert instantiated == [ + ('fixture_1', 'A'), + ('fixture_2', 'a1'), + ('fixture_2', 'a2'), + ('fixture_1', 'B'), + ('fixture_2', 'b1'), + ('fixture_2', 'b2'), + ('fixture_1', 'C'), + ('fixture_2', 'c1'), + ('fixture_2', 'c2'), + ] """ ) result = testdir.runpytest() - result.assert_outcomes(passed=6) + result.assert_outcomes(passed=7)