pytest在派生类中生成测试

时间:2019-03-20 11:42:12

标签: python inheritance pytest code-generation

我需要根据不同的数据生成一些类似的测试。我尝试过

import pytest


class BaseClass(object):
    data = [1]

    @pytest.mark.parametrize("param1", data)
    def test_something(self, param1):
        assert param1


class Test1(BaseClass):
    data = [2, 3]


class Test2(BaseClass):
    data = [0]

但结果是

collected 2 items

test_of_pytest.py::Test1::test_something[1] PASSED                       [ 50%]
test_of_pytest.py::Test2::test_something[1] PASSED                       [100%]

而不是预期的东西:

collected 3 items

test_of_pytest.py::Test1::test_something[2] PASSED                       [ 33%]
test_of_pytest.py::Test1::test_something[3] PASSED                       [ 66%]
test_of_pytest.py::Test2::test_something[0] FAIL                       [100%]

因此,@pytest.mark.parametrize在读取BaseClass时仅运行一次。 如何处理parametrize(或其他生成器)以达到我的期望?

0 个答案:

没有答案