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+requests+allure自动化测试接入Jenkins学习
自动化测试·软件测试·学习·测试工具·职场和发展·jenkins·pytest
丿罗小黑3 小时前
Pytest项目_day20(log日志)
pytest
xt19892884 小时前
pytest+yaml+allure接口自动化测试框架
自动化测试·pytest·接口自动化·框架封装
灰阳阳4 小时前
接口自动化测试大全(python+pytest+allure)
python·pytest·接口自动化·allure
5:003 天前
pytest框架-详解
pytest
丿罗小黑5 天前
Pytest项目_day15(yaml)
pytest
丿罗小黑5 天前
Pytest项目_day16(yaml和parametrize结合)
pytest
代码小念6 天前
Pytest+selenium UI自动化测试实战实例(超详细)
selenium·ui·pytest
丿罗小黑6 天前
Pytest项目_day11(fixture、conftest)
pytest