码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 从0开始python学习-33.夹具@pytest.fixture(scope=““,params=““,autouse=““,ids=““,name=““)


    目录

    1. 创建夹具

    1.1 pytest方式

    1.2 unittest方式

    2. 使用夹具

    2.1 通过参数引用

    2.2 通过函数引用

    3. 参数详解

    3.1 scope:作用域

    3.2 params-参数化

    3.3 autouse=True表示自动使用,默认为False

    3.4 ids:设置变量名

     3.5 name:别名


    1. 创建夹具

    1.1 pytest方式

    1. @pytest.fixture()
    2. def test_a():
    3. print('case执行之前执行')
    4. yield
    5. print('case执行之后执行')

    1.2 unittest方式

    1. class Test:
    2. def setup_method(self):
    3. print('setup_method:case执行之前执行--用例之前执行一次')
    4. def teardown_method(self):
    5. print('teardown_method:case执行之后执行--每个case执行之前均执行')
    6. def setup_class(self):
    7. print('setup_class:case执行之前执行--每个case执行之后均执行')
    8. def teardown_class(self):
    9. print('teardown_class:case执行之后执行--全部用例执行完了之后才执行')

    2. 使用夹具

    2.1 通过参数引用

    2.2 通过函数引用

    3. 参数详解

    @pytest.fixture(scope="",params="",autouse="",ids="",name="")

    3.1 scope:作用域

    表示标记方法的作用域:function(默认),class,module,package,session

    session > module > class > function

    function:每个用例都有一个

    1. @pytest.fixture(scope='function')
    2. def test_a():
    3. print('之前执行')
    4. yield
    5. print('之后执行')
    6. class Test_A:
    7. def test_1(self,test_a):
    8. print('test_1')
    9. def test_2(self,test_a):
    10. print('test_2')
    11. class Test_B:
    12. def test_3(self,test_a):
    13. print('test_3')

    class:每个class 共用一个

    1. @pytest.fixture(scope='class')
    2. def test_a():
    3. print('之前执行')
    4. yield
    5. print('之后执行')
    6. class Test_A:
    7. def test_1(self,test_a):
    8. print('test_1')
    9. def test_2(self):
    10. print('test_2')
    11. class Test_B:
    12. def test_3(self):
    13. print('test_3')

    下面就不举例了:module:每个文件共用一个;package:每个包共用一个;session:全局共用一个

    3.2 params-参数化

    支持列表[]、元组()、字典列表[{},{}],字典元组({},{})

    fixture引用外部参数

    1. param = ['111',[1,2],('a','b')]
    2. @pytest.fixture(params=param)
    3. def test_a(request):
    4. test = request.param
    5. return test
    6. def test_1(test_a):
    7. print(test_a)

    fixture标签直接进行参数化

    1. @pytest.fixture(params=[1,2,'aaaa'])
    2. def test_b(request):
    3. test1 = request.param
    4. return test1
    5. def test_2(test_b):
    6. print(test_b)

     

    3.3 autouse=True表示自动使用,默认为False

    autouse=True时无论是否使用都会被使用

    1. @pytest.fixture(autouse=True)
    2. def test_a():
    3. print('test_a')
    4. def test_1():
    5. print('test_1')

    autouse=False时必须要手动调用了才会被使用

    1. @pytest.fixture(autouse=False)
    2. def test_b():
    3. print('test_b')
    4. def test_2(test_b):
    5. print('test_2')

    3.4 ids:设置变量名

    当使用params参数化时,给每一个值设置一个变量名

    1. param = ['111',[1,2],('a','b')]
    2. @pytest.fixture(params=param,ids=['user1','user2','user3'])
    3. def test_a(request):
    4. test = request.param
    5. return test
    6. def test_1(test_a):
    7. print(test_a)

     3.5 name:别名

    表示被@pytest.fixture标记的方法取一个别名,当取了别名后,原来的名称就不能用了

    1. @pytest.fixture(name='yyyy')
    2. def test_a():
    3. print('11111')
    4. def test_1(yyyy):
    5. print('test_1')
    6. def test_2(test_a):
    7. print('test_1')

  • 相关阅读:
    R语言使用xts包表示时间序列数据(time series data)
    win10 Ubuntu 子系统下编译ffmpeg 和 x264
    达梦8创建schema模式sql无法结束
    Python爬虫深度优化:Scrapy库的高级使用和调优
    操作系统知识点-处理机调度
    安装pytorch总结
    pandas连接oracle数据库并拉取表中数据到dataframe中、筛选当前时间(sysdate)到一个小时之前的所有数据(筛选一个小时的范围数据)
    如何按时间周期保存或备份已处理文件?
    贝塞尔曲线文字路径
    基底和坐标的关系
  • 原文地址:https://blog.csdn.net/Meseiter/article/details/134366617
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号