您现在的位置是:网站首页> 编程资料编程资料
pytest官方文档解读fixtures的autouse_python_
2023-05-26
354人已围观
简介 pytest官方文档解读fixtures的autouse_python_
现在我们已经知道了,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)的执行情况:
- 虽然在测试函数里请求了2个fixture函数,但是order拿到的并不是[],first_entry拿到的也并不是"a"。
- 因为存在了一个autouse=True的fixture函数,所以append_first先会被调用执行。
- 在执行append_first过程中,又分别请求了order、 first_entry这2和fixture函数。
- 接着,append_first对分别拿到的[]和"a"进行append处理,最终返回了["a"]。所以,断言assert order == [first_entry]是成功的。
同理,第二个测试函数test_string_and_int(order, first_entry)的执行过程亦是如此。
以上就是pytest官方文档解读fixtures的autouse的详细内容,更多关于pytest解读fixtures的autouse的资料请关注其它相关文章!
您可能感兴趣的文章:
相关内容
- python中opencv Canny边缘检测_python_
- pytest解读一次请求多个fixtures及多次请求_python_
- Django 状态保持搭配与存储的实现_python_
- pytest官方文档解读fixtures调用fixtures及fixture复用性_python_
- pytest解读fixture有效性及跨文件共享fixtures_python_
- Python获取图像中像素点坐标实例代码_python_
- Python字体反爬实战案例分享_python_
- Python实现基于Fasttext的商品评论数据分类的操作流程_python_
- PyTorch实现手写数字的识别入门小白教程_python_
- python 判断字符串当中是否包含字符(str.contain)_python_
