码农知识堂 - 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')

  • 相关阅读:
    前端体验优化(2)——基建
    ETCD快速入门-02 ETCD安装
    LeetCode 169. 多数元素
    SmartX 边缘计算解决方案:简单稳定,支持各类应用负载
    【数据结构】数组和字符串(五):特殊矩阵的压缩存储:稀疏矩阵——压缩稀疏行(CSR)
    Nginx学习与实战 · 解决net::ERR_CONTENT_LENGTH_MISMATCH 206问题
    Sentinel学习(2)——sentinel的使用,引入依赖和配置 & 对消费者进行流控 & 对生产者进行熔断降级
    Python函数式编程
    前缀和和差分和dijkstra算法和二分算法和floyd算法
    技术分享 | 接口自动化测试如何搞定 json 响应断言?
  • 原文地址:https://blog.csdn.net/Meseiter/article/details/134366617
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号