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

Passing arguments to fixture in TestCase with pytest

如何使用 pytest 将自定义参数传递给 unittest.TestCase 派生 class 的测试方法中的夹具?

在搜索了太长时间之后,我设法发明了使用 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