问题:清理从测试创建的测试工件。在以下情况下,如何使用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?
如果断言在测试中途失败,则在清理结束时,在测试期间添加的行不会被删除。因为测试停止执行。
问题示例是没有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
没有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
在类上使用实例变量的潜在解决方案
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
由灯具产生的对象由跑步者注入到测试环境中。这可以是您想要的任何对象,并且测试可以随意对其进行突变。
这是一种适合您的模式:
@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