zoukankan      html  css  js  c++  java
  • Pytest之自定义mark

    一个完整的项目,测试用例比较多,比如我们想将某些用例用来做冒烟测试,那该怎么办呢?pytest中可以自定义配置文件,用例按照指定的方式去运行。

     

    配置文件

    定义配置文件名

    在项目根目录下,创建一个文件:pytest.ini (固定名称,不要修改)。

     

    配置文件格式
    pytest.ini 

    [pytest]
    markers =
        demo: just for demo
        smoke

    ① 案例一:

    之前在讲解用例被标记为@pytest.mark.xfail时,如果用例运行通过,显示XPASS。

    test_demo.py

    @pytest.mark.xfail()
    def test_demo02():
        print("这是test_demo02")
        assert 1 == 1

    在配置文件中未配置xfail_strict = True时,运行结果如下:

    图片

    在pytest.ini 中加上xfail_strict = True配置后

    运行结果为:

    图片

     

    ② 案例二:addopts

    addopts参数可以更改默认命令行选项,省去手动敲命令行参数。

    比如命令行想输出详细信息、分布式执行或最大失败次数,每次敲命令很麻烦,在配置里设置,以后命令直接输入pytest即可。

    现有如下用例:

    test_demo.py

    def test_demo01():
        print("这是test_demo01")
        assert 1 == 2
    def test_demo02():
        print("这是test_demo02")

    如果需要输出信息更详细、输出调试信息及用例执行错误时立即退出,那么配置如下:

    [pytest]
    markers =
        demo: just for demo
        smoke
    addopts = -v -s -x

    命令行输入:pytest

    输出结果为:

    图片

    测试用例执行实战

    比如我想从众多用例中挑选出部分用例,作为冒烟测试用例,怎么配置呢?

     

    pytest.ini

    [pytest]
    markers =
        demo: just for demo
        smoke
    

      

    其中smoke为标签,用例前加上标签名smoke,即都属于冒烟测试用例。

     

     

    模块级别

    在模块里加上标签,那么该模块下的类、方法或函数都会带上标签。

    test_demo.py

    import pytest
    pytestmark = pytest.mark.smoke
    class TestDemo:
        def test_demo01(self):
            print("这是test_demo01")
        def test_demo02(self):
            print("这是test_demo02")
        def test_demo03(self):
            print("这是test_demo03")

    命令行输入:pytest -v -m smoke

    输出结果为:

    图片

     

    类级别

    在类上添加标签,则类下的所有方法都带上标签

    test_demo.py

    import pytest
    @pytest.mark.smoke
    class TestDemo:
        def test_demo01(self):
            print("这是test_demo01")
        def test_demo02(self):
            print("这是test_demo02")
        def test_demo03(self):
            print("这是test_demo03")
    def test_demo04():
        print("这是test_demo04")

    在命令行输入:pytest -v -m smoke test_demo.py

    图片

     

    函数级别

    在函数上添加标签,那么此函数带上标签。

    test_demo.py

    import pytest
    class TestDemo:
        def test_demo01(self):
            print("这是test_demo01")
        def test_demo02(self):
            print("这是test_demo02")
        def test_demo03(self):
            print("这是test_demo03")
    @pytest.mark.smoke
    def test_demo04():
        print("这是test_demo04")

    命令行输入:pytest -v -m smoke test_demo.py

    输出结果为:

    图片

  • 相关阅读:
    BZOJ 1433 && Luogu P2055 [ZJOI2009]假期的宿舍 匈牙利算法
    BZOJ 1123 && Luogu P3469 [POI2008]BLO-Blockade 割点+乘法原理
    POJ3694 Network 边双缩点+LCA+并查集
    luogu P5142 区间方差 十分优美的线段树
    luogu P2709 小B的询问 最简单的莫队
    luogu P2731 骑马修栅栏 Riding the Fences
    TYVJ P2032 「Poetize9」升降梯上 spfa最短路
    51nod 1515 明辨是非 并查集+set维护相等与不等关系
    BZOJ 1260: [CQOI2007]涂色paint 区间DP
    luogu P4145 上帝造题的七分钟2 / 花神游历各国 维护区间和&&区间开根号
  • 原文地址:https://www.cnblogs.com/chenyablog/p/15171787.html
Copyright © 2011-2022 走看看