• 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

    在这里插入图片描述

  • 相关阅读:
    golang学习笔记——条件表达式
    【ocean】报错*Error* quotient: can‘t handle (nil / nil)
    Django-数据库路由映射
    使用 Sa-Token 实现 [记住我] 模式登录、七天免登录
    python列表和元组的作业
    Android音视频开发(一):通过三种方式绘制图片
    Ubuntu20.04换源教程、解决主机与虚拟机之间进行文本复制粘贴问题
    Docker推送镜像错误
    数据库链接池示实例
    敦煌考古,招不到人了?看数字化技术如何破圈
  • 原文地址:https://blog.csdn.net/mghoumin/article/details/123890889