目录
- Pytest知识点
- 0.代码规范
- 1、右击运行py文件时,以pytest框架运行不是main等故障
- 2、with、contextlib.contextmanager-创建上下文管理器
- 3、allure报告生成
- 4、路径相关
- 5、Pytest.ini
- 6、前置后置
- 7.pytest中装饰器
- 8.yield
- 9.conftest
- 10.pytest_addoption添加自定义命令行
- Pytest钩子函数
- 12.Longging日志模块
- 13.with allure.step()
- 14.迭代器、生成器-省内存
- 15. async协程
- 16.staticmethod
- 17.类方法调用
- 18.self变量
- 19.批处理命令-bat脚本
- 20.优化import方法(__init__.py)
- 21.制作whl文件
- 22.pycharm设置pytest、unittests运行
- pytest
- 基础语法使用
- 初始化清除
- 执行pytest,测试用例
另外一篇文章:基于python实现Web自动化测试(selenium)、API自动化测试(requests)&附学习视频
Pytest知识点
0.代码规范
1下划线使用
1、右击运行py文件时,以pytest框架运行不是main等故障
运行故障:https://zhuanlan.zhihu.com/p/550999600
2、with、contextlib.contextmanager-创建上下文管理器
3、allure报告生成
第一步生成报告的临时文件:json(用于生成html)和txt(用例中的print部分):
pytest --alluredir ./temp
pytest -vs --alluredir ./temp (只生成json)
第二步在本地生成html
allure generate ./temp -o ./report –clean
allure generate 命令,固定的 ./temp 临时的json格式报告的路径
-o 输出output ./report 生成的allure报告路径
–clean 清空report文件夹下原来的报告
第三步也可生成在线html的IP
allure serve ./report
4、路径相关
1.os.walk()找到绝对路径
2. print(file)返回当前py文件绝对路径
3.路径转义符
4.相对路径(…/report)
5、Pytest.ini
1.pytest.ini中testpaths参数
Pytest会自动加载Pytest.ini中的配置信息
2.pytest.ini中markers参数
3.pytest.ini中python_classes参数
4.读写常规ini文件
5.配置与不配置Pytest.ini的区别
6、前置后置
1.setup_method()\teardown_method()\setup_function()
2. pytest.fixture()
1.scope
2.autouse
7.pytest中装饰器
1.@pytest.mark.case2 标记测试用例
2.pytest.mark.parametrize()参数化
3.@pytest.mark.run(order=2)测试运行顺序
4.@pytest.mark.skip()跳过测试用例
5.@pytest.mark.flaky()失败重跑
6.@pytest.mark.repeat(5)重复执行
8.yield
9.conftest
10.pytest_addoption添加自定义命令行
根据terminal处输入不同的命令,执行不同的脚本
参考
Pytest钩子函数
1.pytest_collection_modifyitems 优化用例顺序(用例收集后被调用)
3.pytest_collection_finish(session)(用例收集后被调用)
3. pytest_runtest_makereport获取测试结果(用例执行过程中被调用)
12.Longging日志模块
参考
1.将信息输出到调试框
2.将信息输出到本地
13.with allure.step()
14.迭代器、生成器-省内存
15. async协程
报错:RuntimeWarning: coroutine ‘AutoContextManager.start’ was never awaited
import asyncio
import time
global_flag=True
async def sub_task():
await asyncio.sleep(5)
print("task finished")
async def inner_loop():
global global_flag
while global_flag:
print("in loop")
asyncio.create_task(sub_task())
await asyncio.sleep(3)
async def long_duration():
global global_flag
await asyncio.sleep(20)
global_flag =False
async def test_call():
start_time =time.time()
await asyncio.wait([
asyncio.create_task(long_duration()),
asyncio.create_task(inner_loop()),
])
end_time = time.time()
print("cost time" + str(end_time-start_time))
loop =asyncio.get_event_loop()#创建事件循环
loop.run_until_complete(test_call())#将协程加进去
loop.close() #关闭
16.staticmethod
17.类方法调用
18.self变量
19.批处理命令-bat脚本
20.优化import方法(init.py)
21.制作whl文件
22.pycharm设置pytest、unittests运行
pytest
基础语法使用
1.pytest找寻测试语句
- 从根目录下找test_.py或_test.py的文件
- 从这些文件下,a-找到Test类下的test为前缀的函数,b-或文件下直接test为前缀的函数
- 注意Test类不能写__init__构造函数
2.使用pytest. --找到根目录cmd进入E:\LIN\baiyu_auto>,输入pytest,执行测试,那么此时执行的时baiyu_auto目录下所有的测试用例
4. pytest -s 将每个用例的print语句打印,用于调试
5. pytest -sv更详细的执行信息,包括每个测试类、测试函数的名字
6. pytest case --html=report.html --self-contained-html (case表示E:\LIN\baiyu_auto\case更细目录)-
安装了 pytest-html 插件,这个插件就是用来产生测试报告的,不能加-s,
pytest.mark.parametrize()参数化
allure生成报告
链接:https://pan.baidu.com/s/1OYyn5v_RINqm6R_z3cSv5g
提取码:ruhj
配置环境变量,将E:\LINYANG\Anaconda3\allure-2.15.0\bin加到path中
A-好用
import pytest
import os
#1.指定某一个py
pytest.main(['-s','test_example.py','test_example2.py','--alluredir','./temp'])
os.system('allure generate ./temp -o ./report --clean') #os.system()就相当于在命令行输入
初始化清除
(模块级别,类级别,方法级别,目录级别)
如果没做好,导致某测试用例与A批能跑,与B批不能。
登录测试用例中有数据,pytest自动运行时,导致登录测试用例中数据 串到 用户管理测试用例,信息不准确
模块级别 的初始化、清除 分别 在整个模块的测试用例 执行前后执行,并且 只会执行1次 。
类级别 的初始化、清除 分别 在整个类的测试用例 执行前后执行,并且 只会执行1次
方法级别 的初始化、清除 分别 在类的 每个测试方法 执行前后执行,并且== 每个用例分别执行1次==
执行pytest,测试用例
找到根目录,cmd进入E:\python_project\自动化学习\autotest_pytest>pytest ,输入pytest,执行测试,那么此时执行的是该目录下所有的测试用例,so,要分别测试
1.命令行、terminal中执行pytest
pytest -s 将每个用例的print语句打印,用于调试
pytest -sv更详细的执行信息,包括每个测试类、测试函数的名字
pytest -rA -v 测试结果的简单统计
pytest case --html=report.html --self-contained-html (case表示E:\LIN\baiyu_auto\case更细目录)-
安装了 pytest-html 插件,这个插件就是用来产生测试报告的,不能加-s,加了后,html就不显示print,
- 指定文件名,块里面的函数或者类
pytest case 执行case下所有测试用例
pytest case\登录\test_错误登录1.py -s 只执行错误登录1.py的模块
pytest cases\登录\test_错误登录.py::Test_错误密码 指定模块里面的类
pytest cases\登录\test_错误登录.py::Test_错误密码::test_C001001 指定类里面的方法
pytest cases1 cases2\登录 指定多个目录
- 根据名字
pytest -k C001001 -s 可以是测试函数的名字,可以是类的名字,可以是模块文件名,可以是目录的名字
大小写敏感的不一定要完整,只要能有部分匹配上就行
pytest -k “not C001001” -s not 表示选择名字中不包含
pytest -k “错 and 密码2” -s and 表示选择名字同时包含多个关键字
pytest -k “错 or 密码2” -s 可以用 or 表示选择名字 包含指定关键字之一即可
- 根据标签
冒烟测试时,测试的用例散落在各处,所以要将测试用例打上标签
可以将方法def ,class ,.py 模块设置成标签
2.使用py脚本管理
3.用例执行完后自动生成报告
在pytest.ini中只能有相关的配置,比如在测试结果先清空再保存
[pytest]
addopts= -vs --alluredir=../report --clean-alluredir
可以使用钩子函数来执行相关命令
conftest.py
@pytest.hookimpl(trylast=true)
def pytest_sessionfinish(session):
# 假设你的Allure结果数据在./allure-results目录下,并且你希望将报告生成到./allure-report目录下
command = "allure generate ./allure-results -o ./allure-report --clean"
# 使用os.system执行命令
os.system(command)