Pytest 单元测试框架之标记用例

1、Pytest 中标记用例

  • 接参数 -k 来挑选要执行的测试项
    • pytest -k test_szdcs -s
      • test_szdcs 为函数名称
    • -k 后面接的名称可以为函数名称、类名称、文件名称、目录名称
    • 区分大小写
    • 支持模糊匹配
    • 可以用 not 表示选择用例名称中不包含哪些内容,如下
class Test01():
    def test_szdcs(self):
        print("深圳多测师")
    def test_gzdcs(self):
        print("广州多测师")

class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令    pytest -k "not sz" -s
# 结果如下
test_demo1.py 
广州多测师.
上海多测师.
    • 可以用 and 表示选择的用例名称中同时包含多个关键字,如下
class Test01():
    def test_szdcs(self):
        print("深圳多测师")
    def test_gzdcs(self):
        print("广州多测师")

class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令        pytest -k "g and z" -s
# 结果如下
test_demo1.py 
广州多测师.
    • 可以用 or 表示选择的用例名称中包含其中一个关键字即可,如下
class Test01():
    def test_szdcs(self):
        print("深圳多测师")
    def test_gzdcs(self):
        print("广州多测师")

class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令        pytest -k "sh or sz" -s
# 结果如下
test_demo1.py 
深圳多测师.
上海多测师.
  • 指定标签运行需要运行的测试项
    • @pytest .mark.tag
      • tag 为自定义的标签名称
      • 可以对方法加上标签
      • 也可以对整个类加上标签
      • 也可以定义全局标签
    • pytest test_demo1.py -m tag -s
      • pytest 后面可以接类名称、文件名称、目录名称
      • -m 后面接标签名称
# 对方法加标签
import pytest

class Test01():
    def test_szdcs(self):
        print("深圳多测师")

    @pytest.mark.tag
        # 通过装饰器定义标签,自定义标签名称 tag
    def test_gzdcs(self):
        print("广州多测师")

class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令         pytest test_demo1.py -m tag -s
# 结果如下
test_demo1.py 
广州多测师.
# 对类加标签
import pytest

class Test01():
    def test_szdcs(self):
        print("深圳多测师")

    def test_gzdcs(self):
        print("广州多测师")

@pytest.mark.tag
class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令         pytest test_demo1.py -m tag -s
# 结果如下
test_demo1.py 
上海多测师.
import pytest
# 定义全局标签
mark = pytest.mark.tag

class Test01():
    def test_szdcs(self):
        print("深圳多测师")
    def test_gzdcs(self):
        print("广州多测师")

class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令        pytest -m tag -s
# 结果如下
test_demo1.py 
深圳多测师.
广州多测师.
上海多测师.

2、Pytest 中 skip 跳过用例

  • 标记 skip 表示跳过该用例,不运行
    • pytest.mark.skip(reason="str")
# 在方法上条件装饰器 skip,跳过单条用例
import pytest

class Test01():
    def test_szdcs(self):
        print("深圳多测师")

    @pytest.mark.skip(reason="跳过的用例")
    def test_gzdcs(self):
        print("广州多测师")

class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令         pytest -sv test_demo1.py
# 结果如下
test_demo1.py::Test01::test_szdcs 深圳多测师
PASSED
test_demo1.py::Test01::test_gzdcs 
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多测师
PASSED
# 在类上面添加装饰器 skip,跳过整个类中的所有用例
import pytest

@pytest.mark.skip(reason="跳过的用例")
class Test01():
    def test_szdcs(self):
        print("深圳多测师")

    def test_gzdcs(self):
        print("广州多测师")

class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令        pytest -sv test_demo1.py
# 结果如下
test_demo1.py::Test01::test_szdcs 
SKIPPED
test_demo1.py::Test01::test_gzdcs 
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多测师
PASSED

3、Pytest 中 skipif 跳过用例

  • 通过条件判断是否忽略不执行该用例
  • 判断条件表达式 pytest.mark.skipif(condition,reason="str")
import pytest

class Test01():
    def test_szdcs(self):
        print("深圳多测师")

    @pytest.mark.skipif(2>1,reason="条件正确不执行")
    def test_gzdcs(self):
        print("广州多测师")

class Test02():
    def test_shdcs(self):
        print("上海多测师")

# 运行命令        pytest -sv test_demo1.py
# 结果如下
test_demo1.py::Test01::test_szdcs 深圳多测师
PASSED
test_demo1.py::Test01::test_gzdcs 
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多测师
PASSED

  

原文地址:https://www.cnblogs.com/ZhengYing0813/p/13223455.html