pytest 框架自动化测试

随笔记录

目录

1. 安装 

2. 安装pytest 相关插件

2.1 准备阶段

2.2 安装 

2.3 验证安装成功 

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

3.1 主函数模式

3.1.1 主函数执行指定文件

 3.1.2 主函数执行指定模块

3.1.3 主函数执行某个文件中的某个类、方法、函数

3.1.4 主函数执行生成allure报告

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. Python自动化测试:unittest与pytest框架

    2024-02-19 14:42:01       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-19 14:42:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-19 14:42:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-19 14:42:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-19 14:42:01       20 阅读

热门阅读

  1. 我的创作纪念日

    2024-02-19 14:42:01       29 阅读
  2. P1106 删数问题题解

    2024-02-19 14:42:01       27 阅读
  3. K8S更新部署docker的两种方法举例

    2024-02-19 14:42:01       31 阅读
  4. 设计模式三大原则

    2024-02-19 14:42:01       28 阅读
  5. 设计模式学习笔记 - 学前简述

    2024-02-19 14:42:01       25 阅读
  6. 设计模式的目的

    2024-02-19 14:42:01       29 阅读
  7. 探讨是否存在永远无法解决的情绪问题

    2024-02-19 14:42:01       29 阅读