pytest 框架自动化测试

随笔记录

目录

[1. 安装](#1. 安装)

[2. 安装pytest 相关插件](#2. 安装pytest 相关插件)

[2.1 准备阶段](#2.1 准备阶段)

[2.2 安装](#2.2 安装)

[2.3 验证安装成功](#2.3 验证安装成功)

[3. pytest测试用例的运行方式](#3. pytest测试用例的运行方式)

[3.1 主函数模式](#3.1 主函数模式)

[3.1.1 主函数执行指定文件](#3.1.1 主函数执行指定文件)

[3.1.2 主函数执行指定模块](#3.1.2 主函数执行指定模块)

[3.1.3 主函数执行某个文件中的某个类、方法、函数](#3.1.3 主函数执行某个文件中的某个类、方法、函数)

[3.1.4 主函数执行生成allure报告](#3.1.4 主函数执行生成allure报告)

[3.2 命令行模式](#3.2 命令行模式)


1. 安装

复制代码
1. install pycharm
2. install python 
3. config Envrionment variable

2. 安装pytest 相关插件

2.1 准备阶段
复制代码
# 将以下插件写入 requirements.txt 中

pytest-rerunfailures         #用例失败后重跑
pytest-xdist                 # 测试用例分布式执行,多CPU 分发
pytest-ordering              # 控制用例执行顺序
pytest                       # pytest 框架
pytest-html                  # 生成html格式的自动化测试报告
allure-pytest                 # 用于生成美观的测试报告
2.2 安装
复制代码
terminal 执行 以下命令,一次性安装所有插件:
#  pip install -r .\requirements.txt  
2.3 验证安装成功
复制代码
执行一下命令,验证pytest 安装成功

# pytest

PS D:\Backup\自动化脚本\Riskcop> pytest
================================================================================================================================================== test session starts =================================================================================================================================================== 
platform win32 -- Python 3.7.9, pytest-7.4.0, pluggy-1.0.0
rootdir: D:\Backup\自动化脚本\Riskcop
plugins: allure-pytest-2.13.2, anyio-3.6.1, Faker-18.10.1, assume-2.4.3, forked-1.4.0, html-3.1.1, metadata-2.0.1, ordering-0.6, rerunfailures-10.2, xdist-2.5.0
collected 0 items

================================================================================================================================================= no tests ran in 0.02s ================================================================================================================================================== 
PS D:\Backup\自动化脚本\Riskcop>

3. pytest测试用例的运行方式

3.1 主函数模式
复制代码
#主函数植式
1. 运行所有:pytest.main()

2. 指定模块:
    # pytest main(['-vs','<文件名>'])
    # pytest.main(-vs','test login.py])

3. 指定目录:
    # pytest main(['-vs','<模块名>'])
    # pytest main(-vs','/interface_testcase])


4. 通过nodeid指定用例运行:nodeid由模块名,分隔符(::),类名,方法名,函数名组成。

    4.1 运行指定函数
       # pytest main(['-vs','<模块名>/<文件名>::<方法名>'])
       # pytest.main(['-vs','./interface_testcase/test_interface.py::test_04_func'])

    4.2 运行某个类中的某个方法
       # pytest main(['-vs','<模块名>/<文件名>::<类名>::<方法名>'])
       # pytest main(['-       vs','./interface_testcase/test_interface.py::Testinterface::test_03_zhiliao'])


# 参数详解:
-S:表示输出调试信息,包括print打印的信息
-V:显示更详细的信息
-VS:这两个参数一起用
分隔符- "::"
3.1.1 主函数执行指定文件
复制代码
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :main.py
@说明    :
@时间    :2024/01/22 17:07:32
@作者    :magx
@版本    :1.0
'''

import os
import time
import pytest

# 当前路径 (使用adbpath 方法 可通过dos 窗口执行)
current_path = os.path.dirname(os.path.abspath(__file__))
print('current_path:',current_path)
# 上级目录
father_path = os.path.abspath(os.path.join(current_path,".."))

# json 报告路径
json_report_path = os.path.join(current_path, './Reports/json')
# html 报告路径
html_report_path = os.path.join(current_path, './Reports/html')

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

if __name__ == '__main__':
    '''
    -v : 详细信息 -  文件名:: 类名::方法名:
    -s : 表示输出调试信息,包括print 打印的信息
    '''

    # 方式1:
    # 指定运行文件  "./TestCases/test_AccountLevel_2.py"
    # test_AccountLevel_2.py
    # test_MultiRule_12
    pytest.main(['-vs','./TestCases/test_AccountLevel_2.py'])   # test_AccountLevel_2.py
3.1.2 主函数执行指定模块
复制代码
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :main.py
@说明    :
@时间    :2024/01/22 17:07:32
@作者    :magx
@版本    :1.0
'''

import os
import time
import pytest

# 当前路径 (使用adbpath 方法 可通过dos 窗口执行)
current_path = os.path.dirname(os.path.abspath(__file__))
print('current_path:',current_path)
# 上级目录
father_path = os.path.abspath(os.path.join(current_path,".."))

# json 报告路径
json_report_path = os.path.join(current_path, './Reports/json')
# html 报告路径
html_report_path = os.path.join(current_path, './Reports/html')

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


if __name__ == '__main__':
    '''
    -v : 详细信息 -  文件名:: 类名::方法名:
    -s : 表示输出调试信息,包括print 打印的信息
    '''

    # 方式2: 运行指定模块
    pytest.main(['-vs', './TestCases/'])
3.1.3 主函数执行某个文件中的某个类、方法、函数
复制代码
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :main.py
@说明    :
@时间    :2024/01/22 17:07:32
@作者    :magx
@版本    :1.0
'''

import os
import time
import pytest

# 当前路径 (使用adbpath 方法 可通过dos 窗口执行)
current_path = os.path.dirname(os.path.abspath(__file__))
print('current_path:',current_path)
# 上级目录
father_path = os.path.abspath(os.path.join(current_path,".."))

# json 报告路径
json_report_path = os.path.join(current_path, './Reports/json')
# html 报告路径
html_report_path = os.path.join(current_path, './Reports/html')

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

if __name__ == '__main__':

    '''
    -v : 详细信息 -  文件名:: 类名::方法名:
    -s : 表示输出调试信息,包括print 打印的信息
    '''


    # 方法3: 指定运行某个文件中的某个类、方法、函数
    # 模块名/文件名::函数名
    # 文件名::类名::方法名
    pytest.main(['--vs','./TestCases/test_AccountLevel_2.py::cleanlog'])
3.1.4 主函数执行生成allure报告
复制代码
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :main.py
@说明    :
@时间    :2024/01/22 17:07:32
@作者    :magx
@版本    :1.0
'''

import os
import time
import pytest

# 当前路径 (使用adbpath 方法 可通过dos 窗口执行)
current_path = os.path.dirname(os.path.abspath(__file__))
print('current_path:',current_path)
# 上级目录
father_path = os.path.abspath(os.path.join(current_path,".."))

# json 报告路径
json_report_path = os.path.join(current_path, './Reports/json')
# html 报告路径
html_report_path = os.path.join(current_path, './Reports/html')

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

if __name__ == '__main__':

    '''
    -v : 详细信息 -  文件名:: 类名::方法名:
    -s : 表示输出调试信息,包括print 打印的信息
    '''

    # ===================================================================================
    # --alluredir生成json格式报告
    # allure generate 使用generate命令导出html报告,json_report_path json格式报告路径, -o生成报告到文件夹, --clean清空原来的报告
    #执行pytest下的用例并生成json文件

    pytest.main(['-vs', './TestCases','--alluredir=%s' %json_report_path, '--clean-alluredir'])#, '--clean-alluredir'
    # 把json文件转成html报告
    os.system('allure generate %s -o %s --clean' %(json_report_path, html_report_path))
3.2 命令行模式
复制代码
# 命令行模式
1. 运行所有:pytest
2. 指定模块:
    #  pytest -vs <文件名>
    #  pytest -vs test_login.py
3. 指定目录:
    #  pytest-vs  <模块名>
    #  pytest-vs ./interface_testcase
4. 指定目录:
   通过nodeid指定用例运行:nodeid由模块名,分隔符(::),类名,方法名,函数名组成

    #  pytest-vs ./interface testcase/test interface.py:test 04_func
相关推荐
西游音月4 天前
(2)pytest+Selenium自动化测试-环境准备
selenium·测试工具·pytest
我的xiaodoujiao4 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 23--数据驱动--参数化处理 Yaml 文件
python·学习·测试工具·pytest
我的xiaodoujiao7 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 22--数据驱动--参数化处理 Json 文件
python·学习·测试工具·pytest
胜天半月子8 天前
Python自动化测试 | 快速认识并了解pytest的基本使用
服务器·python·pytest
北珣.12 天前
自动化框架pytest基础
自动化·pytest
程序员杰哥13 天前
Pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
学习3人组13 天前
Python + requests + pytest + allure + Jenkins 构建完整的接口自动化测试框架
python·jenkins·pytest
shao91851616 天前
Gradio全解14——使用Gradio构建MCP的服务器与客户端(4)——Python包命令:uv与uvx实战
pytest·uv·1024程序员节·npx·uvx·uv pip·ruff
我的xiaodoujiao17 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 20--PO(POM) 设计模式和用例撰写
python·学习·测试工具·设计模式·pytest
sky0Lan18 天前
一个类似 pytest 的 html 报告
android·html·pytest