【开发也是好测试】(三)—pytest fixture scope
来源:互联网 发布:bw报表软件 编辑:程序博客网 时间:2024/06/05 16:11
导读
本文在http://pythontesting.net/framework/pytest/pytest-session-scoped-fixtures/ 文章的基础上,归纳总结fixture的不同scope的区别。
不同scope含义
- session
- module
- class
- function
代码说明
(借用了参考文章中的代码)
conftest.py:
import pytest@pytest.fixture(scope="session")def resource_a(request): print('In resource_a()') def resource_a_fin(): print('\nIn resource_a_fin()') request.addfinalizer(resource_a_fin)@pytest.fixture(scope="module")def resource_b(request, resource_a): print('In resource_b()') def resource_b_fin(): print('\nIn resource_b_fin()') request.addfinalizer(resource_b_fin)@pytest.fixture(scope="function")def resource_c(request, resource_b): print('In resource_c()') def resource_c_fin(): print('\nIn resource_c_fin()') request.addfinalizer(resource_c_fin)# these are just some fun dividiers to make the output pretty# completely unnecessary, I was just playing with autouse fixtures@pytest.fixture(scope="function", autouse=True)def divider_function(request): print('\n --- function %s() start ---' % request.function.__name__) def fin(): print(' --- function %s() done ---' % request.function.__name__) request.addfinalizer(fin)@pytest.fixture(scope="module", autouse=True)def divider_module(request): print('\n ------- module %s start ---------' % request.module.__name__) def fin(): print(' ------- module %s done ---------' % request.module.__name__) request.addfinalizer(fin)@pytest.fixture(scope="session", autouse=True)def divider_session(request): print('\n----------- session start ---------------') def fin(): print('----------- session done ---------------') request.addfinalizer(fin)
test_one_two.py:
def test_one(resource_c): print('In test_one()')def test_two(resource_c): print('\nIn test_two()')
test_three_four.py:
def test_three(resource_c): print('\nIn test_three()')def test_four(resource_c): print('\nIn test_four()')
output:
$ py.test -s -v==================================== test session starts ====================================platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2 -- /usr/bin/pythoncollected 4 items test_one_two.py:1: test_one ----------- session start --------------- ------- module test_one_two start --------- --- function test_one() start ---In resource_a()In resource_b()In resource_c()In test_one()PASSEDIn resource_c_fin() --- function test_one() done ---test_one_two.py:4: test_two --- function test_two() start ---In resource_c()In test_two()PASSEDIn resource_c_fin() --- function test_two() done ---In resource_b_fin() ------- module test_one_two done ---------test_three_four.py:1: test_three ------- module test_three_four start --------- --- function test_three() start ---In resource_b()In resource_c()In test_three()PASSEDIn resource_c_fin() --- function test_three() done ---test_three_four.py:4: test_four --- function test_four() start ---In resource_c()In test_four()PASSEDIn resource_c_fin() --- function test_four() done ---In resource_b_fin() ------- module test_three_four done ---------In resource_a_fin()----------- session done ---------------================================= 4 passed in 0.02 seconds ==================================
在这次执行中一种有4个case,属于同一个session,两个module,4个function,如下表所示
test_one执行时,session开始,test_one_two.py这个module开始,test_one function开始,test_two执行完成时,function和module范围结束,而直到test_four执行结束时,session才结束。
阅读全文
0 0
- 【开发也是好测试】(三)—pytest fixture scope
- 【开发也是好测试系列】(二)——pytest之fixture
- 【开发也是好测试系列】(一)——用pytest编写Test Case
- pytest fixture
- 【Pytest】fixture函数介绍(重点)
- 【开发也是好测试】(四)—Mock
- pytest的fixture
- pytest的fixture
- 一个好用的python测试框架(pytest)及其CI设置
- Google C++单元测试框架(Gtest)系列教程之三——测试固件(Test fixture)
- Google C++单元测试框架(Gtest)系列教程之三——测试固件(Test fixture)
- Google C++单元测试框架(Gtest)系列教程之三——测试固件(Test fixture)
- 使pytest fixture 实现setup、teardown、多次执行
- python 测试框架 pytest
- Python pytest测试
- 12.python开源——pytest自动化测试框架
- fixture
- 使用pytest+requests进行接口测试实践——测试get请求
- 计算机网络传输层介绍
- 当帆软的报表设计与泛微OA的流程结合是一个经典的集成
- 如何在Promise链中共享变量?
- #Android学习# 简单的轮播图的实现[上]
- NKOJ 2439 四叶草魔杖(最小生成树+状压dp/网络流)
- 【开发也是好测试】(三)—pytest fixture scope
- keepalived+nginx高可用环境搭建
- 0904 servlet
- 已解决:mybatis 查询默认过滤值为空的字段
- 来来来,一起聊聊互联网行业的黑话,你知道哪些?
- poj1664 放苹果
- 刷题训练——PAT(Basic Level)-1010
- 最短路 Dijkstra+优先队列与SPFA 模版
- PHP 常用函数笔记