pytest五:fixture_autouse=True
平常写自动化用例会写一些前置的 fixture 操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传返个参数,会比较麻烦。
fixture 里面有个参数 autouse,默讣是 Fasle 没开启的,可以设置为True 开启自动使用 fixture 功能,返样用例就丌用每次都去传参了
调用 fixture 三种方法
1.函数或类里面方法直接传 fixture 的函数参数名称
2.使用装饰器@pytest.mark.usefixtures()修饰
3.autouse=True 自动使用
方法一:先定义 start 功能,用例全部传 start 参数,调用该功能

import pytest
@pytest.fixture(scope='function')
def start(request):
print('\n=========开始执行function============') def test_a(start):
print('=======用例a==========') class TestCase(): def test_01(self, start):
print('=======用例01=========') def test_02(self, start):
print('=======用例02=========') if __name__=='__main__':
pytest.main() 方法二:使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例


import pytest @pytest.fixture(scope='function')
def start(request):
print('==========开始执行function===========') @pytest.mark.usefixtures('start')
def test_a():
print('==============执行用例a===============') @pytest.mark.usefixtures('start')
class TestCase(): def test_01(self):
print('============用例01============') def test_02(self):
print('============用例02============') if __name__=='__main__':
pytest.main()
方法三:autouse 设置为 True,自动调用 fixture 功能
1.start 设置 scope 为 module 级别,在当前.py 用例模块只执行一次,autouse=True 自动使用
2.open_home 设置 scope 为 function 级别,每个用例前都调用一次,自动使用



import time
import pytest @pytest.fixture(scope='module', autouse=True)
def start(request):
print('\n==========开始执行modlue=========')
print(f'module: {request.module.__name__}')
print('========启动浏览器========')
yield
print('===========结束测试===========') @pytest.fixture(scope='function', autouse=True)
def open_home(request):
print(f'function:{request.function.__name__}\n========回到首页======') def test_01():
print('==========用例01==========') def test_02():
print('==========用例02==========') # 第一种方法:类里面写
class TestCase():
@pytest.fixture(scope='function', autouse=True)
def open_home(self, request):
print(f'function:{request.function.__name__}\n========回到首页=======') def test_01(self):
print('==========类用例01============') def test_02(self):
print('==========类用例02============') # 第二种方法:也可以使用 @pytest.mark.usefixtures('open_home') 调用
@pytest.mark.usefixtures('open_home')
class TestCase1(): def test_01(self):
print('==========类用例01============') def test_02(self):
print('==========类用例02============') if __name__=='__main__':
pytest.main()
pytest五:fixture_autouse=True的更多相关文章
- 学习-Pytest(五)yield操作
1.fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作 2.scope="module" 1.fixture参数scope=”modu ...
- Pytest系列(7) - skip、skipif跳过用例
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest.mark.sk ...
- pytest测试框架 -- assert断言和fixture固件
一.断言 (1)使用assert语句进行断言 # test_run.py @pytest.mark.assert def test_assert(self): r = requests.get(&qu ...
- 2.pytest前后置(固件、夹具)处理
一.setup/teardown/setup_calss/teardown_class 为什么需要这些功能? 比如:我们执行用例之前,需要做的哪些操作,我们用例执行之后,需要做哪些操作 # 在所有用例 ...
- flask-sqlalchemy、pytest 的单元测试和事务自动回滚
flask-sqlalchemy.pytest 的单元测试和事务自动回滚 使用 flask-sqlalchemy 做数据库时,单元测试可以帮助发现一些可能意想不到的问题,像 delete-cascad ...
- pytest进阶之fixture
前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...
- 【Python】【自动化测试】【pytest】
https://docs.pytest.org/en/latest/getting-started.html#create-your-first-test http://www.testclass.n ...
- pytest 学习笔记一:参数化与组织分层
组织分层: 1.普通方式,和unittest分层类似: setup_module() # 通常放在类外 setup_class(cls) setup(self) teardown(self) tea ...
- pytest的参数化测试
感觉在单元测试当中可能有用, 但在django这种框架中,用途另一说. import pytest import tasks from tasks import Task def test_add_1 ...
随机推荐
- js位操作
1.&(与) 都是1才是1 例如:14&15 (14 二进制 1110 15 二进制 1111 &与的结果 1110 ---->结果14) (14& ...
- python---ORM之SQLAlchemy(1)
定义一个类,ORM(对象关系映射)将这个类转换为sql语句,使用pymysql进行执行 一,底层处理 使用engine/connectionpooling/dialect进行数据库操作,engine使 ...
- 构建eureka-server异常ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistrationBean
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistr ...
- jenkins多个slave遇到的坑
jenkins配置如下: 在Jenkins上添加了两个节点(Slave Node),且为这两个节点设置了一个相同的标签"windows".创建了一个新Job – "tes ...
- TCP和UDP的区别和优缺点
1.TCP与UDP区别总结: 1.TCP面向连接(如打电话要先拨号建立连接);UDP是无连接的,即发送数据之前不需要建立连接2.TCP提供可靠的服务.也就是说,通过TCP连接传送的数据,无差错,不丢失 ...
- codeforces - 432D Prefixes and Suffixes (next数组)
http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...
- Scapy Fuzz实现——S7协议从建连到“正常交流“(一)
转载:安全客 酝酿了“三秒钟“,准备理清逻辑写写我学习的心得,自认为和Siemens S7协议有过一段时间浅浅的“交流”,所以这过程中涉及到了自己整理的自认为有用的东西,涉及工具.脚本这般,发出来让大 ...
- Linux - 文件ACL权限控制
getfacl 1.test # 查看文件ACL权限 setfacl -R -m u:xuesong:rw- 1.test # 对文件增加用户的读写权限 -R 递归
- postman变量的使用和设置
之前只是使用postman做接口管理——将各个项目使用到的接口分类管理起来,用的时候手动改参数调用.这次项目连着跑三个接口,需要用到前一个接口的参数,还来回切平台,真的很麻烦,所以就搜了一下有什么便利 ...
- B - SETI POJ - 2065 (高斯消元)
题目链接:https://vjudge.net/contest/276374#problem/B 题目大意: 输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'* ...