[英]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.