使用pytest将参数传递给TestCase中的Fixture

时间:2019-02-26 19:38:34

标签: python pytest fixtures

如何使用 pytest 将自定义参数传递给unittest.TestCase派生类的测试方法内的装置?

1 个答案:

答案 0 :(得分:0)

搜索了很长时间之后,我设法使用doc和有关包装夹具的线程发明了解决方案。我希望有人会觉得有用。

conftest.py

import pytest

@pytest.fixture()
def add(request):
    def wrapped(a=10, b=5):
        return a + b
    request.cls.add = wrapped

add_test.py

import pytest
from unittest import TestCase

@pytest.mark.usefixtures('add')
class AddTestCase(TestCase):
    def test_add(self):
        # parameters can be passed inside test method
        result = self.add(2, 2) 
        assert result == 4