# 该txt为说明文档,描述框架简介以及一些代码写作规范等
一. 测试框架简介
整个测试框架如下:
base:存放的是请求方法二次封装
common:存放的是自定义工具方法等,操作excel,yaml文件等
data:存放的是配置文件,如 testdata.yaml
log:存放的是Log日志信息
report:存放的是测试报告
testcase:存放的是测试用例
main: 用例执行
explain: 框架整体说明书,描述框架简介以及一些代码写作规范等
二. 方法介绍
待补充
三. 写作规范
1. 模块名必须以test_开头或者_test结尾
2. 测试类必须以Test开头,并且不能有init方法(构造函数的测试类找不到)
3. 测试方法必须以test开头
4. 首字母大写+大写式驼峰
5. 全局变量、常量,全部使用大写字母+下划线驼峰
四. ini文件设置
如果因为某种需要,需要使用其他命名规则命名的测试文件、测试函数、测试类以及测试类的方法,可以通过pytest.ini配置文件做到。
pytest.ini文件是pytest的主配置文件,可以改变pytest的默认行为。
1.pytest.ini的放置位置:一般放在项目工程的根目录(即当前项目的顶级文件夹下)
2.编码格式必须是ANSI,可以使用notpad++修改编码格式
3.pytest.ini的作用:指定pytest的运行方式(在cmd输入pytest后,会读取pytest.ini中的配置信息,按指定的方式去运行)
4.cmd下使用 pytest -h 命令查看pytest.ini的设置选项
5.定义smoke,在设置里面新增markers标记
常用设置选项如下:
[pytest]
addopts = -s … #可添加多个命令行参数,用空格分隔
testpaths = …/pytestproject #测试用例文件夹,可自己配置,…/pytestproject为上一层的pytestproject文件夹。
python_files = test*.py #配置测试搜索的模块文件名称
python_classes = Test* #配置测试搜索的测试类名
python_funtions = test #配置测试搜索的测试函数名
markers = smoke #配置smoke用例连跑机制,可以单独跑冒烟用例
######################################
[pytest]
addopts = -s --html=./report.html
testpaths = …/pytestproject
python_files = test*.py
python_classes = Test*
python_funtions = test*
#######################################
五. 使用说明
运行模式详解
单独执行某个 Python 文件:
pytest 文件.py
单独执行某个文件中的某个类
pytest 文件.py::类名
单独执行某个文件中某个类的某个方法
pytest 文件.py::类名::方法名
参数说明
1、-s:显示打印内容
如:pytest pytest-demo.py -s
等价于:pytest.main([’-s’,‘pytest-demo.py’])
2、:::指定测试用例运行
运行函数:如:pytest pytest-demo.py::test_01
等价于:pytest.main([’-s’,‘pytest-demo.py::test01’])
运行类中方法:如:pytest pytest-demp.py::TestCase::test_03
等价于:pytest.main([’-s’, ‘pytest-demo.py::TestCase::test_03’])
3、–html=路径/report.html:生成xml/html格式测试报告(需要先安装pytest-html)
如:pytest pytest-demp.py --html-./report.html
等价于:pytest.main([’-s’,‘pytest-demo.py’,’–html=./report.html’])
4、–maxfail=1:出现1个失败就终止测试
如:pytest pytest-demo.py --maxfail=1
等价于:pytest.main([’-s’,‘pytest-demo.py’,’–maxfail=1’])
5、-n:pytest-xdist多线程运行(需要先安装pytest-xdist)
如:pytest pytest-demo.py -n 2
等价于:pytest.main([’-s’,‘pytest-demo.py’,’-n=2’])
在用例中使用time.sleep(2)加等待时间测试,发现多线程时间减少了。
6、–reruns NUM:重试运行测试用例(需要先安装pytest-rerunfailures)
如:pytest pytest-demo.py --reruns 2
则一旦有用例失败,则会重复执行两次,貌似这种不能用main运行。
7. -x 只有一个用例失败,就不执行
8. -k 包含某个标记的用例执行
如:pytest pytest-demo.py -k "cj4"
9. 无条件跳过
如: @pytest.mark.skip(reason='我想跳过他') # 无条件跳过
10. 有条件跳过
如: @pytest.mark.skipif(num<20, reason='num的数组过小,跳过')
用例模板设置:
# -*- conding:utf-8 -*-
"""
@Time: ${DATE} ${TIME}
@Auth: 帅气的长江
@Email: chaojiangliu2@163.com
@Content: {$NAME}.py
"""
六. 工具下载
allure: 下载官网上2.13.0, 版本过高可能使用不会生成测试报告。(具体可能跟版本有关,版本不兼容?)
其余依赖包,可以通过package.txt文档中的包下载,或者直接使用该文档提供的命令