Pytest框架中pytest.mark功能

作者 : admin 本文共2872个字,预计阅读时间需要8分钟 发布时间: 2024-06-17 共1人阅读

Pytest框架中pytest.mark功能插图

文章目录

mark功能

1. 使用@pytest.mark.skip

2. 使用@pytest.mark.skipif

3. 使用 @pytest.mark.xfail

4使用@pytest.mark.parametrize

5 使用@pytest.mark.自定义标记

6 使用@pytest.mark.usefixtures

pytest 的mark功能在pytest官方文档是这样解释的:
https://docs.pytest.org/en/latest/how-to/mark.html

Pytest框架中pytest.mark功能插图(1)

由于英语不好,只能百度翻译如下:

通过使用pytest.mark助手,您可以轻松地设置测试函数的元数据。您可以在API参考中找到内置标记的完整列表。或者,您可以使用CLI-pytest-标记列出所有标记,包括内置标记和自定义标记。

以下是一些内置标记:

usefixtures-在测试函数或类上使用fixtures

filterwarnings-筛选测试函数的某些警告

skip-始终跳过测试函数

skipif-如果满足特定条件,则跳过测试函数

xfail-如果满足特定条件,则产生“预期失败”结果

parameterize-对同一测试函数执行多个调用。

创建自定义标记或将标记应用于整个测试类或模块很容易。这些标记可以由插件使用,也通常用于通过-m选项在命令行上选择测试。

我们可以通过 @pytest.mark装饰器来为测试函数做标记。

1 mark功能

使用@pytest.mark的功能时,我们跳到原始代码文件中,看到其实用到的如下类

Pytest框架中pytest.mark功能插图(2)

下面主要介绍几个常用的风mark功能

1. 使用@pytest.mark.skip

@pytest.mark.skip 装饰器可以用来无条件地跳过某个测试。

import pytest

@pytest.mark.skip
def test_1():
  assert True

def test_2():
  assert True

以上用例执行结果:test_1不会被执行

Pytest框架中pytest.mark功能插图(3)

2. @pytest.mark.skipif

与 @pytest.mark.skip 不同,@pytest.mark.skipif 允许我们基于某个条件来跳过测试。如果给定的条件为真,则测试会被跳过。

举例:根据python版本判断是否执行某用例。

import pytest
import sys
def value_a_b():
  if sys.version_info < (3, 8):
     return 'a'
  else:
     return 'b'
@pytest.mark.skipif(str(value_a_b()=='b'),reason='当值为b时不执行用例')
def test_1():
   assert True

def test_2():
   assert True

用例执行结果如下:当前python版本为3.10,当函数返回value_a_b返回值为‘b’时,用例被skip。

Pytest框架中pytest.mark功能插图(4)

使用 @pytest.mark.xfail

该功能标记一个测试用例为预期失败。

举例:标记测试用例test_1为预期失败用例

@pytest.mark.xfail(reason="该用例执行会失败")
def test_1():
   assert False

def test_2():
   assert True

用例执行结果如下:

Pytest框架中pytest.mark功能插图(5)

4使用@pytest.mark.parametrize

可以为测试函数提供多个输入和期望的输出,从而在一次运行多个测试用例

语法:

@pytest.mark.parametrize(“arg1, arg2”, [(“value1″,”value2” ), (“value3”, “value4”), …])

  • arg1, arg2:这些是在测试函数内部使用的参数名
  • [(“value1″,”value2” ), (“value3”, “value4”), …]:这是一个包含元组的列表,每个元组对应一组测试参数。

举例:用例中传入2个参数arg1和arg2,对应3组参数

import pytest

@pytest.mark.parametrize("arg1, arg2", [(1,2), (3,4),(5,6)])
def test_1(arg1,arg2):
   print(f'
arg1+arg2 = {arg1+arg2}')

执行后结果:运行了3条用例

Pytest框架中pytest.mark功能插图(6)

5 @pytest.mark.自定义标记

除了上述的内置标记外,我们可以使用 @pytest.mark 装饰器为测试用例添加标记。例如标记测试用例为version1_1_0,我们可以使用 -m version1_1_0来只运行这些测试用例。

使用方式跟-k参数类似,可以填and 或者or等表达式符号。

举例:将test_1和test_2标记为version1_1_0,将test_3标记为version1_1_1

#函数1
@pytest.mark.version1_1_0
def test_1():
  assert 1==1
  print('
用例test_1执行成功')

class TestCase(): 
  @pytest.mark.version1_1_0
  def test_2(self): #函数2
     assert 1 == 1
     print('
用例test_2执行成功') @pytest.mark.version1_1_1
  
  def test_3(self): #函数3
     assert 1 == 1
     print('
用例test_3执行成功')

pytest.main([‘-v’,’-m version1_1_0′,’test_case3.py’]),执行结果如下:只执行被标记的用例test_1,test_2

Pytest框架中pytest.mark功能插图(7)

6 使用@pytest.mark.usefixtures

在测试类和测试函数中使用@pytest.mark.usefixtures运行定义的fixture函数

举例:定义一个fixture

@pytest.fixture(scope='class')
def class_setup_teardown():
  print("
Class setup (equal to setup_class)")
  # 设置代码 
  yield
  print("
Class teardown (equal to teardown_class)")
  # 清理代码

在测试类中注明使用上面定义的 fixture

@pytest.mark.usefixtures("class_setup_teardown")
class Testcase:
  def test_one(self):
    print("
Running test_one")
    # 测试代码
  def test_two(self):
    print("
Running test_two")
    # 测试代码

用例执行后结果如下:两种方式都实现了测试类setup和teardown的功能

Pytest框架中pytest.mark功能插图(8)

共勉: 东汉·班固《汉书·枚乘传》:“泰山之管穿石,单极之绠断干。水非石之钻,索非木之锯,渐靡使之然也。”

—–指水滴不断地滴,可以滴穿石头;

—–比喻坚持不懈,集细微的力量也能成就难能的功劳。

—-感谢读者的阅读和学习,点个赞和关注,谢谢大家

本站无任何商业行为
个人在线分享 » Pytest框架中pytest.mark功能
E-->