[英]How to implement pytest fixture and parametrized arguments as arguments on the same test?
[英]Reusing pytest fixture in the same test
以下是使用user
夾具設置測試的測試代碼示例。
@pytest.fixture
def user():
# Setup db connection
yield User('test@example.com')
# Close db connection
def test_change_email(user):
new_email = 'new@example.com'
change_email(user, new_email)
assert user.email == new_email
如果我想例如添加一些功能來批量更改用戶電子郵件,並且需要在測試之前設置10個用戶,是否可以使用同一夾具在同一測試中生成多個用戶對象?
pytest文檔的“ 工廠作為固定裝置 ”部分解決了我的問題。
特別是此示例(從鏈接復制/粘貼):
@pytest.fixture
def make_customer_record():
created_records = []
def _make_customer_record(name):
record = models.Customer(name=name, orders=[])
created_records.append(record)
return record
yield _make_customer_record
for record in created_records:
record.destroy()
def test_customer_records(make_customer_record):
customer_1 = make_customer_record("Lisa")
customer_2 = make_customer_record("Mike")
customer_3 = make_customer_record("Meredith")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.