problems with fixtures’ scope in pytest

  fixtures, pytest, python, testing

I have a problems with fixtures’ scope in test class.
I have 4 types of items: type1_old, type1_new, type2_old and type2_new.
I want to start all test class functions for one type of item. At begin for type1_new then type1_old then type2_new then type2_old.

Now tests run like:

test_1.py::TestDifferentFixtures::test_1[item_1_type1_new_no_3d] PASSED                                                                       [  6%]
test_1.py::TestDifferentFixtures::test_1[item_2_type1_old_no_3d] PASSED                                                                       [ 12%]
test_1.py::TestDifferentFixtures::test_1[item_3_type2_new_no_3d] PASSED                                                                       [ 18%]
test_1.py::TestDifferentFixtures::test_1[item_4_type2_old_no_3d] PASSED                                                                       [ 25%]
test_1.py::TestDifferentFixtures::test_2[item_13_type1_new_no_3d_unvalid] PASSED                                                              [ 31%]
test_1.py::TestDifferentFixtures::test_2[item_14_type1_old_no_3d_unvalid] PASSED                                                              [ 37%]
test_1.py::TestDifferentFixtures::test_2[item_15_type2_new_no_3d_unvalid] PASSED                                                              [ 43%]
test_1.py::TestDifferentFixtures::test_2[item_16_type2_old_no_3d_unvalid] PASSED                                                              [ 50%]
test_1.py::TestDifferentFixtures::test_3[item_5_type1_new_with_3d] PASSED                                                                     [ 56%]
test_1.py::TestDifferentFixtures::test_3[item_6_type1_old_with_3d] PASSED                                                                     [ 62%]
test_1.py::TestDifferentFixtures::test_3[item_7_type2_new_with_3d] PASSED                                                                     [ 68%]
test_1.py::TestDifferentFixtures::test_3[citem_8_type2_old_with_3d] PASSED                                                                    [ 75%]
test_1.py::TestDifferentFixtures::test_4[item_9_type1_new_no_value] PASSED                                                                    [ 81%]
test_1.py::TestDifferentFixtures::test_4[item_10_type1_old_no_value] PASSED                                                                   [ 87%]
test_1.py::TestDifferentFixtures::test_4[item_11_type2_new_no_value] PASSED                                                                   [ 93%]
test_1.py::TestDifferentFixtures::test_4[item_12_type2_old_no_value] PASSED                                                                   [100%]

But I hope:

test_1.py::TestDifferentFixtures::test_1[item_1_type1_new_no_3d] PASSED
test_1.py::TestDifferentFixtures::test_2[item_13_type1_new_no_3d_unvalid] PASSED
test_1.py::TestDifferentFixtures::test_3[item_5_type1_new_with_3d] PASSED
test_1.py::TestDifferentFixtures::test_4[item_9_type1_new_no_value] PASSED

test_1.py::TestDifferentFixtures::test_1[item_2_type1_old_no_3d] PASSED
test_1.py::TestDifferentFixtures::test_2[item_14_type1_old_no_3d_unvalid] PASSED
test_1.py::TestDifferentFixtures::test_3[item_6_type1_old_with_3d] PASSED
test_1.py::TestDifferentFixtures::test_4[item_10_type1_old_no_value] PASSED


test_1.py::TestDifferentFixtures::test_1[item_3_type2_new_no_3d] PASSED
test_1.py::TestDifferentFixtures::test_2[item_15_type2_new_no_3d_unvalid] PASSED
test_1.py::TestDifferentFixtures::test_3[item_7_type2_new_with_3d] PASSED
test_1.py::TestDifferentFixtures::test_4[item_11_type2_new_no_value] PASSED


test_1.py::TestDifferentFixtures::test_1[item_4_type2_old_no_3d] PASSED
test_1.py::TestDifferentFixtures::test_2[item_16_type2_old_no_3d_unvalid] PASSED
test_1.py::TestDifferentFixtures::test_3[citem_8_type2_old_with_3d] PASSED
test_1.py::TestDifferentFixtures::test_4[item_12_type2_old_no_value] PASSED

Fixtures (./fixtures/fixtures_for_test.py):

import pytest


def items_no_3d(fixture_value):
    items = (
        "item_1_type1_new_no_3d",
        "item_2_type1_old_no_3d",
        "item_3_type2_new_no_3d",
        "item_4_type2_old_no_3d"
    )
    return items[fixture_value]


def items_with_3d(fixture_value):
    items = (
        "item_5_type1_new_with_3d",
        "item_6_type1_old_with_3d",
        "item_7_type2_new_with_3d",
        "citem_8_type2_old_with_3d"
    )
    return items[fixture_value]


def items_with_no_value(fixture_value):
    items = (
        "item_9_type1_new_no_value",
        "item_10_type1_old_no_value",
        "item_11_type2_new_no_value",
        "item_12_type2_old_no_value",
    )
    return items[fixture_value]


def items_with_no_3d_unvalid(fixture_value):
    items = (
        "item_13_type1_new_no_3d_unvalid",
        "item_14_type1_old_no_3d_unvalid",
        "item_15_type2_new_no_3d_unvalid",
        "item_16_type2_old_no_3d_unvalid",
    )
    return items[fixture_value]


@pytest.fixture(params=[i for i in range(4)], ids=items_no_3d)
def item_no_3d_valid(request):
    return items_no_3d(request.param)


@pytest.fixture(params=[i for i in range(4)], ids=items_with_no_3d_unvalid)
def item_no_3d_unvalid(request):
    return items_with_no_3d_unvalid(request.param)


@pytest.fixture(params=[i for i in range(4)], ids=items_with_3d)
def items_with_3ds(request):
    return items_with_3d(request.param)


@pytest.fixture(params=[i for i in range(4)], ids=items_with_no_value)
def items_no_value(request):
    return items_no_3d(request.param)

Test class (./tests/test_1.py):

import pytest
from fixtures.fixtures_for_test import *


class TestDifferentFixtures:

    def test_1(self, item_no_3d_valid):
        pass

    def test_2(self, item_no_3d_unvalid):
        pass

    def test_3(self, items_with_3ds):
        pass

    def test_4(self, items_no_value):
        pass

Source: Python Questions

LEAVE A COMMENT