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
相关推荐
开源优测1 天前
Pytest中5种不同的方法解析JSON数据
log4j·json·pytest
不求大富大贵只求富可敌国1 天前
Gtest, Junit,以及pytest对比理解
junit·pytest
xing25162 天前
pytest下放pytest.ini文件就导致报错:ERROR: file or directory not found: #
pytest
xing25164 天前
pytest-html
前端·html·pytest
唐古乌梁海5 天前
【pytest】编写自动化测试用例命名规范README
自动化·pytest
xing25165 天前
pytest下allure
开发语言·python·pytest
IT求学人6 天前
pytest运行用例的常见方式及参数
pytest
开源优测7 天前
什么是pytest.ini及如何在Pytest中应用以提升配置效率
pytest
摸鱼仙人~7 天前
ImportError: cannot import name ‘FixtureDef‘ from ‘pytest‘
conda·pytest·fastapi