码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • pytest+allure 详细版


    一、Pytest -断言、跳过及运行

    1、mark中的skip

    @pytest.mark.skip()和@pytest.mark.skipif()

    1、被标记的类中所有方法测试用例都会被跳过
    2、被标记的类,当条件为ture时,会被跳过,否则不跳过

    import pytest
    
    @pytest.mark.skip()
    def test_demo1():
    
        a = 10
        assert a == 10
    
    
    @pytest.mark.skipif(1 == 1, reason="环境不支持")
    def test_demo2():
        m = 1
        assert m == 1
    
    if __name__ == '__main__':
        pytest.main(["-sv"])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2、mark中的xfail(失败)

    含义是:将该用例标记成xfail失败,并且该用例中的后续代码不会执行。
    用例中pytest.xfail()方法之前的代码运行了,之后的不再运行;结果中有一条用例被标记为xfail

    import pytest
    
    
    def test_demo1():
        print("------ start ------")
        pytest.xfail(reason="功能没有完成")
        print("demo1 执行方法")
        assert 1 == 1
    
    
    
    def test_demo2():
        print("----- demo2 执行方法------")
        assert 'o' in 'love'
    
    if __name__ == '__main__':
        pytest.main(["-sv"])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    xfai还有一种使用方法。就是@pytest.mark.xfail标签
    import pytest
    
    @pytest.mark.xfail
    def test_demo1():
        print("------ start ------")
        print("demo1 执行方法")
        assert 1 == 1
    
    
    def test_demo2():
        print("----- demo2 执行方法------")
        assert 'o' in 'love'
    
    
    if __name__ == '__main__':
        pytest.main(["-sv"])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    3、使用自定义标记mark只执行部分用例

    import pytest
    
    
    @pytest.mark.app
    def test_demo1():
        print("app demo1 执行方法")
    
    
    @pytest.mark.web
    def test_demo2():
        print("web demo2 执行方法")
    
    
    @pytest.mark.app
    def test_demo3():
        print("app demo3 执行方法")
    
    
    if __name__ == '__main__':
        pytest.main([ "test_test1.py","-sv", "-m=web"])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    4、文件名类名方法执行部分用例

    只执行符合要求的某一部分用例,通过类与方法的

    二、pytest-fixture

    1、fixture可以当做参数传入

    定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开。fixture是有返回值得,没有返回值默认为None。用例调用fixture的返回值,直接就是把fixture的函数名称当做变量名称

    import pytest
    
    
    @pytest.fixture()
    def test_demo1():
        m = 10
        return m
    
    
    def test_demo2(test_demo1):
        assert test_demo1 == 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

  • 相关阅读:
    金属标记/荧光标记/功能化改性/官能团表面包覆聚苯乙烯微球
    如何解决 npm ERR! Cannot read properties of null (reading ‘pickAlgorithm‘)报错问题
    sql server 安装时“无法在COM+目录中安装和配置程序”的解决办法
    【详细】Java网络通信 TCP、UDP、InetAddress
    绁炵粡缃戠粶杈撳叆鍥剧墖澶у皬
    TSINGSEE青犀视频汇聚机房动环智能监控方案,提升机房安全稳定性
    Java高并发(线程创建以及线程池,异步调用,CompletableFuture)
    代码随想录算法训练营第四十三天【动态规划part05】 | 1049. 最后一块石头的重量 II、494. 目标和、474.一和零
    国际阿里云如何远程连接服务器设备管理系统!!
    神经网络入门(上)
  • 原文地址:https://blog.csdn.net/mghoumin/article/details/123890889
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    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号