遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(12):mark标记测试用例

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

前言

通常,我们通过分包或者分模块来对用例进行分类管理,

如果只想执行符合某要求的部分用例,该如何实现呢?

可以使用装饰器@pytest.mark.xxx给用例打标签(自定义标记)。

 

自定义标记使用流程

1. 注册自定义标记(通过pytest.ini进行管理)

2. 将模块、函数、类、方法进行业务标记

3. 根据自定义标记运行用例

 

注册自定义标记

在pytest.ini文件中,新建标记:

[pytest]
markers =
    user: user marker
    product: product marker
    findproduct
    deleteproduct
    modulex: modulex marker

 

说明:

1.标记名要是英文命名,建议参考模块命名,要有业务含义
2.所有自定义标记,建议在pytest.ini中进行统一管理,并通过命令参数--strict-markers进行授权(非必须)
3.pytest中的markers配置相当于我们对用例的一种归类

 

开启严格标记,如果标记不在配置文件中,会报错;

不开启严格标记,如果标记不在配置文件中,会warning;

[pytest]
addopts = --strict-markers
markers =
    user: user marker
    product: product marker
    findproduct
    deleteproduct
    modulex: modulex marker

 

获取现有标记: pytest --markers,包含自定义和内置,前面几个是我们刚刚创建的

 

自定义标记分类

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

pytestmark = pytest.mark.modulex # 模块类标记
# pytestmark = [pytest.mark.modulex, pytest.mark.moduley]  # 可以注册多个

@pytest.fixture(params=['韧','全栈测试笔记','性能测试'])
def fun(request):  # 必须是request这个参数名
    return request.param  # 依次取列表中的每个值返回

@pytest.fixture(params=['apple','car'])
def fun2(request):
    return request.param
@pytest.mark.user  # 函数标记
def test_user_reg(fun):
    print(f"---test_user_reg: {fun}")

@pytest.mark.user
def test_user_login():
    print("---test_user_login")

@pytest.mark.product  # 类标记
class TestProduct:
    @pytest.mark.findproduct  # 方法标记
    def test_product_find(self, fun2):
        print(f"---test_product_find: {fun2}")

    @pytest.mark.deleteproduct
    def test_product_delete(self):
        print("---test_product_delete")

 

说明:

  模块、函数、方法、类上都可以标记多个。

  模块标记方式是:

pytestmark = [pytest.mark.modulex, pytest.mark.moduley]

  函数、方法、类标记方式是:

@pytest.mark.module1
@pytest.mark.module2

 

根据标记运行用例

[pytest]
addopts = -s --strict-markers
markers =
    user: user marker
    product: product marker
    findproduct
    deleteproduct
    modulex: modulex marker

  

运行所有:

 

指定marker运行:

  pytest case\test_qzcsbj.py -m user

  或者:pytest case\test_qzcsbj.py -m=user

 


指定多个marker运行:

  pytest case\test_qzcsbj.py -m "user or deleteproduct"

  或者:pytest case\test_qzcsbj.py -m="user or deleteproduct"

 


排除某个marker运行:
  pytest case\test_qzcsbj.py -m "not user"

  或者:pytest case\test_qzcsbj.py -m="not user"

 

posted @ 2024-02-23 21:28  全栈测试笔记  阅读(240)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end