Python unittest.TestCase类中的参数化夹具
我正在使用pytest,并且希望使用参数化pytest夹具。如果我不使用类并运行pytest,两个测试都会通过,但在测试类中,两个测试都会失败,但我无法找出错误所在 工作代码:Python unittest.TestCase类中的参数化夹具,python,python-3.x,unit-testing,pytest,python-unittest,Python,Python 3.x,Unit Testing,Pytest,Python Unittest,我正在使用pytest,并且希望使用参数化pytest夹具。如果我不使用类并运行pytest,两个测试都会通过,但在测试类中,两个测试都会失败,但我无法找出错误所在 工作代码: import pytest @pytest.fixture def db_fixture(): def insert_records(data): return data db = insert_records yield db db = {} # reset fixt
import pytest
@pytest.fixture
def db_fixture():
def insert_records(data):
return data
db = insert_records
yield db
db = {} # reset fixture
def test_create_record_one(db_fixture):
result = db_fixture({"id": 1})
assert result == {"id": 1}
def test_create_record_two(db_fixture):
result = db_fixture({"id": 2})
assert result == {"id": 2}
不起作用的代码:
import pytest
import unittest
class TestSample(unittest.TestCase):
@staticmethod
@pytest.fixture
def db_fixture():
def insert_records(data):
return data
db = insert_records
yield db
db = {} # reset fixture
@staticmethod
def test_create_record_one(db_fixture):
result = db_fixture({"id": 1})
assert result == {"id": 1}
@staticmethod
def test_create_record_two(db_fixture):
result = db_fixture({"id": 2})
assert result == {"id": 2}
错误:
TypeError:test\u create\u record\u one()缺少1个必需的位置参数:“db\u fixture”
您不能将unittest.TestCase
与pytest
fixture混合使用。虽然pytest
知道unittest
并可以基于unittest.TestCase
执行有效的单元测试,但情况并非如此
在您的情况下,不要从unittest.TestCase
派生:
类测试示例:
@静力学方法
@pytest.fixture
def db_夹具():
def insert_记录(数据):
返回数据
db=插入\u记录
收益率分贝
db={}#重置夹具
@静力学方法
def测试\创建\记录\一个(db\ U夹具):
结果=db_夹具({“id”:1})
断言结果=={“id”:1}
...