当前位置: 首页 > news >正文

pytest框架_简介、pytest.main()

1、pytest单元测试框架干了啥?

(1)从多个文件中找到测试用例文件
(2)按照一定规则去执行,并生成结果
(3)通过断言判断预期结果
(4)统计测试进度,耗时、通过率,生成测试报告

2、pytest基础介绍:

(1)非常成熟的单元测试框架,可以和selenium、requests、appium结合实现web自动化、接口自动化、app自动化。
(2)pytest兼容unittest(毛坯房)框架,unittest框架迁移pytest框架不需要重写代码。
(3)可以实现测试用例的跳过、失败用例重试
(4)和allure生成美观的测试报告,比如:错误的截图
(5)与jenkins持续集成
(6)pytest(精装房,300多插件实现了实用的操作)
(7)常用插件:
pytest-html 生成html测试报告
pytest-xdist 测试用例分布式执行
pytest-ordering 测试用例的执行顺序
pytest-rerunfailures 用例失败后重跑
allure-pytest 美观的测试报告
如何一次性安装插件?
将以下内容放到根目录下的requirements.txt中,通过Terminal栏目pip install -r requirements.txt,文件内容:
pytest-html
pytest-xdist
pytest-ordering
pytest-rerunfailures
allure-pytest
pytest

(8)pytest用例编写规则是什么?
测试用例层全部以test开头,别问,问就是以test开头。文件、类、方法统统以test开头,类的首字母默认大写。你问我为啥???pytest代码逻辑就是这,你不以这开头,代码去哪找你的测试用例。还有一个,不能有init方法。
在这里插入图片描述

3、pytest运行方式

(1)测试用例执行方式1:
命令行中Terminal中输入【pytest -vs test_2.py】

(2)测试用例执行方式2:
令定义文件,用pytest.main()方法执行,示例:suit.py

# coding=utf-8
import pytest

if __name__ == '__main__':

    # # 执行测试用例目录(TestCase)下,所有test开头的.py文件
    pytest.main(['-vs'])
    pytest.main()

    # 执行测试用例目录(TestCase)下test_1.py的文件,这个文件跟test_2在一层
    pytest.main(['-vs','test_2.py'])

    # 执行interface_testcase目录下面的test开头的用例
    pytest.main(['-vs', 'D:\\se_frame\\interface_testcase'])

    # 失败的用例再跑2次,运用插件:pytest_rerunfailures
    pytest.main(['-vs', 'test_2.py', '--reruns=2'])

    # 执行下test_2.py下的testlogin类下的test_case1方法
    pytest.main(['-vs', 'test_2.py::Testlogin::test_004'])

    # 分布式运行,执行2个用例。运用插件:pytest-xdist
    pytest.main(['-vs', 'test_2.py', '-n=2'])

    # 根据用例的部分字符串指定测试用例,ao是用例的方法名内容
    pytest.main(['-vs', 'test_2.py', '-k=ao'])
    pytest -vs test_2.py -k "ao"

    """
    分布式结果是这样的:
    test_2.py::Testlogin::test_case1 
    test_2.py::Testlogin::test_case2 
    [gw1] PASSED test_2.py::Testlogin::test_case2 
    [gw0] PASSED test_2.py::Testlogin::test_case1 
    test_2.py::Testlogin::test_case4 
    test_2.py::Testlogin::test_case3 
    [gw0] PASSED test_2.py::Testlogin::test_case3 
    [gw1] FAILED test_2.py::Testlogin::test_case4 
    """3)测试用例执行方式3:通过pytest.ini运行,核心配置文件,
    # 位置:项目根目录,
    # 编码格式:ANSI编码(notepad++转格式)
    # 运行规则:主函数、命令行执行用例都会读取配置文件
    # 文件内容:干掉标注,转成ANSI格式
    """
    [pytest]
    # pytest执行用例的参数,空格分开,这里加了-vs,你用pytest执行用例,就不用加了
    addopts = -vs --html ./report/report.html
    # 测试用例文件夹,可以自己配置
    testpaths = D:\se_frame\TestCases
    # 配置测试搜索的测试类名
    python_classes = Test*
    # 配置测试搜索的测试函数名
    python_functions = test
    # 分模块执行用例
    markers = 
        smoke:冒烟用例
        usermange:用户管理模块
        product:产品模块
    """

# 执行用例的顺序是:
    # 按照@pytest.mark.run(order=2)这个order的值来排序

#如何执行冒烟用例?分模块执行?分接口?分web?>>>配置文件
# (1)用例上加标记 @pytest.mark.xxx 例如冒烟用例:@pytest.mark.smoke
# (2)配置文件进行配置
# # 执行了冒烟用例、product模块
# pytest.main([’-vs’, ‘test_2.py’, ‘-m= smoke or product’])

4、生成测试报告?

第一种报告:pytest.ini加--html 路径。运行用例即可生成测试报告,举例如下:
pytest配置文件详解:https://blog.csdn.net/weixin_45451320/article/details/113916427
pytest.ini
参数详解:
-s 表示输出信息,包括print打印的信息
-v 表示更相信的信息
-vs 一般一起用
-n 支持分布式运行测试用例
-k 根据用例的部分字符串指定测试用例
--html 路径 生成测试报告

"""
[pytest]
addopts = -vs --html D:\\se_frame\\Reports\\report.html
testpaths = D:\se_frame\TestCases
python_classes = Test*
python_functions = test
markers =
    smoke:冒烟用例
    usermange:用户管理模块
    product:产品模块
"""
# 第二种报告:
allure-pytest【项目基本用这个,自动截图失败用例,自动记录失败异常,美观好看各种好】
https://blog.csdn.net/weixin_45451320/article/details/113916870

5、pytest.main()示例:

# coding=utf-8
import pytest
import time


class Testlogin:

    # 装饰器:标记用例执行的顺序,用的插件:pytest-ordering
    @pytest.mark.run(order=2)
    @pytest.mark.smoke
    def test_case_03(self):
        time.sleep(3)
        print('一只小黄狗')

    @pytest.mark.product
    @pytest.mark.run(order=1)
    def test_case_02(self):
        time.sleep(3)
        print('一只小红狗')

    @pytest.mark.run(order=3)
    def test_case_04(self):
        time.sleep(3)
        print('一只小绿狗')

    @pytest.mark.run(order=4)
    def test_case4_01_ao(self):
        time.sleep(3)
        print('一只小花狗')
        assert 1==2

# def test_004():
#     print('函数')


相关文章:

  • pytest框架_@pytest.fixture()
  • pytest框架_@pytest.mark.usefixtures()
  • pytest框架_conftest.py详解
  • pytest框架_@pytest.mark.parametrize()
  • pytest框架_@pytest.yield_fixture()
  • pytest框架_@pytest.mark.xfail()
  • pytest框架_@pytest.mark.skipif()
  • pytest框架_pytest.ini
  • pytest框架_测试报告(allure-pytest)
  • pytest框架_pytest_runtest_makereport实现失败截图自动加入allure
  • pytest框架_assert断言
  • python_获取共享文件里的内容
  • 算法基础_二分查找
  • 算法基础_数组、链表、选择排序
  • selenium_alert、confirm、prompt弹框、上传文件的弹框
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • Android 架构优化~MVP 架构改造
  • CODING 缺陷管理功能正式开始公测
  • css属性的继承、初识值、计算值、当前值、应用值
  • JavaScript服务器推送技术之 WebSocket
  • KMP算法及优化
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • Making An Indicator With Pure CSS
  • Map集合、散列表、红黑树介绍
  • Sass 快速入门教程
  • Wamp集成环境 添加PHP的新版本
  • 闭包,sync使用细节
  • 飞驰在Mesos的涡轮引擎上
  • 判断客户端类型,Android,iOS,PC
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 区块链共识机制优缺点对比都是什么
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 系统认识JavaScript正则表达式
  • 优秀架构师必须掌握的架构思维
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • 函数计算新功能-----支持C#函数
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • #define,static,const,三种常量的区别
  • #ifdef 的技巧用法
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (附源码)php新闻发布平台 毕业设计 141646
  • (十一)手动添加用户和文件的特殊权限
  • (算法)前K大的和
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转贴)用VML开发工作流设计器 UCML.NET工作流管理系统
  • .describe() python_Python-Win32com-Excel
  • .gitignore文件_Git:.gitignore
  • .NET CLR基本术语
  • .NET 的程序集加载上下文
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)