将参数传递给 pytest 夹具进行拆卸

Dan*_*ler 4 fixtures pytest

问题: 清理从测试中创建的测试工件。在下面的情况下,如何使用 pytest 固定装置从数据库中删除在测试期间创建的单行?(并不是每次运行后都应该从表中删除所有内容。否则可以使用删除所有行或删除表)。创建的行的行标识符在测试期间保存在函数变量中。

是否可以将测试期间创建的变量作为参数传递到 pytest 中的夹具中?无论测试是通过失败还是成功完成,夹具都需要始终运行。在运行测试之前不会知道行标识符。

用夹具说明的问题

@pytest.fixture()
def clean_up_db_row(row_id):
    yield
    delete_from_db(self.row_id). # code to delete the row based on the id


def test_something_added_to_database(clean_up_db_row):
    row_id = create_db_row()  # function under test
    ...
    assert row_id in db  # test that information added to the database

    # the clean_up_db_row fixture will always run but how will it know about the id variable defined in the function?

Run Code Online (Sandbox Code Playgroud)

如果断言在测试中途失败,则在将清理工作拖到最后时,在测试期间添加的行不会被删除。因为测试停止执行。

问题的示例是没有 pytest 固定装置:

def clean_up_db_row(row_id):
    yield
    delete_from_db(row_id). # code to delete the row based on the id


def test_something_added_to_database():
    row_id = create_db_row()  # function under test
    ...
    assert row_id in db  # test that information added to the database
    clean_up_db_row(row_id)  # this won’t run if there is a failure
Run Code Online (Sandbox Code Playgroud)

没有pytest夹具的解决方案


def clean_up_db_row(row_id):
    yield
    delete_from_db(row_id). # code to delete the row based on the id

def test_something_added_to_database():
    row_id = create_db_row()  # function under test
    ...
    try:
        assert row_id in db  # test that information added to the database
    except Exception as e:
        raise e
    finally:
        clean_up_db_row(row_id)  # this will always run but doesn’t use a fixture

Run Code Online (Sandbox Code Playgroud)

在类上使用实例变量的潜在解决方案

class TestCaseCleanUp:

    @pytest.fixture(autouse=True)
    def clean_up_db_row(self):
        yield
        delete_from_db(self.row_id). # code to delete the row based on the id


    def test_something_added_to_database(self):
        self.row_id = create_db_row()  # function under test
        ...
        assert self.row_id in db  # test that information added to the database
        # the autouse fixture can use the self.row_id assigned
Run Code Online (Sandbox Code Playgroud)

wim*_*wim 6

由夹具产生的对象由运行器注入到测试上下文中。这可以是您想要的任何对象,并且测试可以自由地改变它。

这是一个应该适合您的模式:

@pytest.fixture()
def clean_up_db_rows():
    row_ids = []
    yield row_ids
    for row_id in row_ids:
        delete_from_db(row_id)


def test_something_added_to_database(clean_up_db_rows):
    row_id = create_db_row()  # function under test
    clean_up_db_rows.append(row_id)
    ...
    assert row_id in db  # test that information added to the database
Run Code Online (Sandbox Code Playgroud)