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
相关推荐
莫负初6 小时前
Pytest 使用Pycharm右键直接运行测试脚本正常,控制台命令pytest运行收集不到用例无法正常测试 no tests ran in 0.01s
ide·pycharm·pytest
欲游山河十万里1 天前
pytest(三)——参数化@pytest.mark.parametrize
pytest
奶茶精Gaaa5 天前
pytest
pytest
霍格沃兹测试开发学社测试人社区7 天前
软件测试学习笔记丨Pytest 学习指南
软件测试·笔记·测试开发·学习·pytest
神即道 道法自然 如来9 天前
测试面试题:pytest断言时,数据是符点类型,如何断言?
pytest
high_tea9 天前
pytest - 多线程提速
python·pytest
傻啦嘿哟11 天前
自动化测试框架集成:将Selenium集成到pytest与unittest中
selenium·测试工具·pytest
一名在八月份找工作的测试员11 天前
自动化学习1:pytest自动化框架的基本用法:注意事项/断言assert/测试结果分析
学习·自动化·pytest
什么时候才能变强12 天前
Pytest-如何将allure报告发布至公司内网
linux·服务器·pytest