简体   繁体   English

如何在@ pytest.mark.parametrize上使用类变量

[英]how to use class variable on @pytest.mark.parametrize

I have a testcase named test_case_params . 我有一个名为test_case_params的测试用例。 It is required to pass some parameters (getting mock data from class Mock_data ) to a fixture and check the return value in the testcase. 需要将一些参数(从class Mock_data获取模拟数据)传递给fixture并检查测试用例中的返回值。 Do I have another option to use Mock_data.get_mock_data in @pytest.mark.parametrize and the testcase? 我还有另一个选项可以在@pytest.mark.parametrize和testcase中使用Mock_data.get_mock_data吗? The purpose is to make sure that all testcases are using the same mock data. 目的是确保所有测试用例都使用相同的模拟数据。 What I did so far is creating a mock_data variable so that I can use it across all testcases since the entire Testsuit is inherited from TestFixure. 到目前为止我所做的是创建一个mock_data变量,以便我可以在所有测试用例中使用它,因为整个Testsuit都是从TestFixure继承的。 But I am not sure how I can write the pytest.mark.parametrize part. 但我不知道如何编写pytest.mark.parametrize部分。

import pytest
class Mock_data:
    @staticmethod
    def get_mock_data():
        return [1,2,3,4,5]

class TestFixure(object):
    @pytest.fixture(scope='function')
    def func_fixture(self, request):
        print('#do something in fixure{}'.format(request.param))
        return request.param

        def fin():
            print('#do something on finalizer')
        request.addfinalizer(fin)

    @classmethod
    def setup_class(cls):
        super(TestFixure, cls).setup_class()
        cls.mock_data = Mock_data.get_mock_data()

    @classmethod
    def teardown_class(cls):
        super(TestMember, cls).teardown_class()


class TestSuite(TestFixure):
    @pytest.mark.parametrize('func_fixture', [Mock_data.get_mock_data()[1], Mock_data.get_mock_data()[2], Mock_data.get_mock_data()[3]], indirect=['func_fixture'])
    def test_case_params(self, func_fixture):
        print(func_fixture)
        assert func_fixture in self.mock_data

Your entire class TestFixure looks broken to me. 你的全班TestFixure看起来很TestFixure Lines like super(TestFixure, cls).setup_class() suggest that you intend to derive it from something ... ? super(TestFixure, cls).setup_class()表明你打算从某些东西派生出来......?

I had a hard time figuring something like this out for the first time myself, so I rewrote your code example. 我自己很难第一次搞清楚这样的事情,所以我重写了你的代码示例。 I believe it does what you want and I hope it helps you to understand how the concepts of fixture and parametrization works: 我相信它能满足你的需求,我希望它能帮助你理解夹具和参数化的概念是如何工作的:

import pytest

class __mock_data_class__:

    def __init__(self):
        self.vector = [2, 3, 4, 5, 6]

    def do_something(self, parameter):
        return self.vector[parameter] - 2

    def fin(self):
        self.vector.clear() # demo action at the end ...

@pytest.fixture(scope = 'function')
def mock_data(request):

    mock_data_object = __mock_data_class__()

    def __finalizer__():
        mock_data_object.fin()

    request.addfinalizer(__finalizer__)

    return mock_data_object

class TestSuite():

    @pytest.mark.parametrize('some_parameter', range(0, 5))
    def test_case_params(self, mock_data, some_parameter):
        print(some_parameter)
        assert some_parameter == mock_data.do_something(some_parameter)

Your mock data is generated (or fetched or ...) by a class named __mock_data_class__ . 您的模拟数据由名为__mock_data_class__的类生成(或获取或...)。 I will be initialized as mock_data once per test case (there is only one in your example, test_case_params ) thanks to scope = 'function' . 我将在每个测试用例中初始化为mock_data (在您的示例中只有一个, test_case_params ),这要归功于scope = 'function' The object will hold your data in a "field" named vector . 该对象将您的数据保存在名为vector的“字段”中。 The mock_data object lives until the test case has been called for every parameter passed into it ( range(0, 5) equals a generator for [0, 1, 2, 3, 4] ). mock_data对象一直存在,直到为每个传递给它的参数调用测试用例( range(0, 5) mock_data range(0, 5)等于[0, 1, 2, 3, 4] )。 At the end of its life, it will "destroy" the data (just as an example) with self.vector.clear() in the class method fin . 在它的生命结束时,它将使用类方法fin中的self.vector.clear() “破坏”数据(仅作为示例)。 Your test case can do something directly in the mock_data object by calling do_something . 您的测试用例可以通过调用do_something直接在mock_data对象中执行某些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM