Pytest系列(8)

摘要:
转移自:https://www.cnblogs.com/poloyy/一、前言:pytest可以支持用户定义的标记,它可以将一个web项目划分为多个模块,然后指定执行的模块名称。例如,我可以指出哪些用例在窗口下执行,哪些用例在mac下执行。运行代码时,我可以指定标记。2.实际代码#/usr/bin/envpython#-*-coding:utf-8-*-importpytest@pytes

转自:https://www.cnblogs.com/poloyy/

一、前言

  • pytest 可以支持自定义标记,自定义标记可以把一个 web 项目划分多个模块,然后指定模块名称执行
  • 譬如我可以标明哪些用例是window下执行的,哪些用例是mac下执行的,在运行代码时候指定mark即可
二、实际代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pytest


@pytest.mark.weibo
def test_weibo():
    print("测试微博")


@pytest.mark.toutiao
def test_toutiao():
    print("测试头条")


@pytest.mark.toutiao
def test_toutiao1():
    print("再次测试头条")


@pytest.mark.xinlang
class TestClass:
    def test_method(self):
        print("测试新浪")


def testnoMark():
    print("没有标记测试")

2.1 cmd敲运行命令

pytest -s -m weibo 08_mark.py

2.2 执行结果

img

2.3 如何避免 warnings

  • 创建一个pytest.ini文件(后续详解)
  • 加上自定义mark,如下图
  • 注意:pytest.ini需要和运行的测试用例同一个目录,或在根目录下作用于全局
[pytest]
markers =
    weibo: this is weibo page
    toutiao: this is toutiao page
    xinlang: this is xinlang page

​ 或者

def pytest_configure(config):
    marker_list = ["weibo","toutiao","xinlang"]
    for markers in marker_list:
        config.addinivalue_line("markers",markers)

2.4 如果不想标记 weibo 的用例,not

pytest -s -m "not weibo" 08_mark.py

2.5 如果想执行多个自定义标记的用例,or

pytest -s -m "toutiao or weibo" 08_mark.py

免责声明:文章转载自《Pytest系列(8)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JEECMS站群管理系统-- 标签使用和模板的制作Prometheus集群+alertmanager集群+influxDB远程存储,实现监控高可用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CentOS下安装pytest

要使用easy_install安装pytest,应该先在CentOS下安装setuptools,通过如下命令完成: yum install python-setuptools 安装完setuptools后,执行:easy_install pytest 即可完成pytest安装...

unittest与pytest的区别

1、pytest可以根据标签自动设别测试用例 2、断言简洁 assert+表达式,而unittest需调用内部函数,如assertEqual 3、pytest可设置会话级、模块级、类级、函数级的fixtures,即用yield分割:数据准备和数据清理工作(前置后置) 4、pytest有多种插件可以集成,如reruns重运行、allure测试报告的集成等。...

pytest框架之allure报告生成

一、关于安装   allure是跟pytest一起集成使用的,所以需要同时安装pytest以及allure-pytest插件: pip install pytest pip install allure-pytest   接下来安装allure,它依赖java环境,需要提前安装jdk并配置jdk环境变量,环境变量如果不会配置的,可以跳转到这个地址:htt...

pytest自学第二期

好,现在开始学习pytest第二章2 pytest初级用法2.1 通过python解释器调用 pytest 在控制台输入命令:   python -m pytest 文档说这种方式相当于直接调用 pytest,唯一的不同是这样调用会把当前目录添加到sys.path (这句是重点,重点是啥,我不知道,   我不知道这有什么用c 以后就这样,如果有自己学过但是...

pytest 1.简单介绍一,安装和如何运行

一、pytest是一个接口测试框架,试用版起来比较轻便灵活。首先来介绍他的安装: 直接使用命令 : pip install -U pytest 通过命令 :pytest --version  来查看版本信息 二、首先来创建第一个简单的demo,可以在pycharm里面创建,并且运行,运行只需要配置一下就可以 # content of test_1.py d...

Pytest自动化测试

Allure除了具有Pytest基本状态外,其他几乎所有功能也都支持。 1、严重性 如果你想对测试用例进行严重等级划分,可以使用 @allure.severity 装饰器,它可以应用于函数,方法或整个类。 它以 allure.severity_level 枚举值作为参数,分别为:BLOCKER(中断),CRITICAL(严重),NORMAL(常规),...