0

I have the following test file

import pytest

def my_list_imp():
    return [1, 2, 3, 4, 5]

class TestParent:
    @pytest.mark.parametrize("item", my_list_imp())
    def test_item(self, item):
        print("item: ", item)
        assert item < 6

What is the proper way to inherit the TestParent class in another file and override only my_list_imp (so it can return a different list)? How to design the parent and child class, so only the list with items is overriden in the child?

If I make my_list_imp a method of the class, I do not know how to call it inside "parametrize" argument?

I need to use parametrize in order to execute the tests in parallel.

Thanks in advance

Jaxx
  • 1,355
  • 2
  • 11
  • 19

1 Answers1

0

There is no straightforward way of doing this, you can not call methods of the instance of the class inside parameterize. The closest approach of applying different parameters for inherited test is to use pytest_generate_tests:

def pytest_generate_tests(metafunc):
    if metafunc.cls is not None:
        funcarglist = metafunc.cls.params[metafunc.function.__name__]
        metafunc.parametrize('item', funcarglist)


class TestParent:
    params = {
        "test_item": [1, 2, 3, 4, 5]
    }

    def test_item(self, item):
        print("item: ", item)
        assert item < 6


class TestChild(TestParent):
    params = {
        "test_item": [-1, -2, -3, -4, -5]
    }
Andrei Evtikheev
  • 331
  • 2
  • 12