Python 官方文档:入门教程 => 点击学习
现在我们已经知道了,fixtures是一个非常强大的功能。 那么有的时候,我们可能会写一个fixture,而这个fixture所有的测试函数都会用到它。 那这个时候,就可以用auto
现在我们已经知道了,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)的执行过程亦是如此。
以上就是pytest官方文档解读fixtures的autouse的详细内容,更多关于pytest解读fixtures的autouse的资料请关注编程网其它相关文章!
--结束END--
本文标题: pytest官方文档解读fixtures的autouse
本文链接: https://www.lsjlt.com/news/118505.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0