现在我们已经知道了,fixtures是一个非常强大的功能。
那么有的时候,我们可能会写一个fixture,而这个fixture所有的测试函数都会用到它。
那这个时候,就可以用autouse自动让所有的测试函数都请求它,不需要在每个测试函数里显示的请求一遍。
具体用法就是,将autouse=True传递给fixture的装饰器即可。
import pytest
@pytest.fixture
def first_entry():
return "a"
@pytest.fixture
def order(first_entry):
return []
@pytest.fixture(autouse=True)
def append_first(order, first_entry):
return order.append(first_entry)
def test_string_only(order, first_entry):
assert order == [first_entry]
def test_string_and_int(order, first_entry):
order.append(2)
assert order == [first_entry, 2]
先来看第一个测试函数test_string_only(order, first_entry)的执行情况:
同理,第二个测试函数test_string_and_int(order, first_entry)的执行过程亦是如此。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理