Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python unittest.TestCase类中的参数化夹具_Python_Python 3.x_Unit Testing_Pytest_Python Unittest - Fatal编程技术网

Python unittest.TestCase类中的参数化夹具

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

我正在使用pytest,并且希望使用参数化pytest夹具。如果我不使用类并运行pytest,两个测试都会通过,但在测试类中,两个测试都会失败,但我无法找出错误所在

工作代码:

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}
...