python +pytest+allure笔记方便自己copy
pytest一些用法
-
import pytest
-
#测试文件以test_开头(以_test结尾也可以)
-
#测试类以Test开头,并且不能带有 init 方法
-
#测试函数以test_开头
-
"""
-
pytest插件
-
pytest-html ==生成html格式的测试报告
-
pytest-xdist 多线程
-
pytest-ordering 指定测试用例的执行顺序
-
pytest-rerunfailures 失败重执行
-
allure-pytest 生成allure测试报告
-
复制python 包
-
pip freeze > requirements.txt
-
PS:在pytest项目下创建一个a.txt文件,将这些参数放进去,使用 pip install -r a.txt 一次性全部安装
-
==============================================================================================
-
1.pytest 参数化
-
@pytest.mark.parametrize()
-
1.1单个参数化
-
1.2多个参数化
-
2.pytest执行测试用例的顺序
-
2.1 默认顺序:默认从上到下
-
2.2自定义顺序 ps:需下载插件pip install pytest-ordering
-
3.跳过用例执行
-
@pytest.mark.skip('跳过说明,可不填')
-
@pytest.mark.skipif(条件成立跳过,reason=)
-
@pytest.mark.skipif(con == '冒烟', reason='我是skipif,因为条件成立被跳过了')
-
4.失败不计入失败用例中
-
@pytest.mark.xfail()
-
5.参数传递
-
5.1单个参数 将上一个方法里面的返回传递给下一个方法,调用的时候传入方法名
-
pytest.fixture()
-
5.2多个参数
-
定义一个全局变量和一个空列表,将需要返回的值追加到列表中然后return返回列表,下一个方法再遍历列表
-
6.pytest执行参数详解
-
-r 用于skip方法中打印不执行的理由
-
-v 显示更详细的信息
-
-s 输出调试信息,包括print打印的信息,一般 -vs一起用
-
-n 支持多线程或者分布式运行测试用例
-
'-n=2' 2个线程
-
--reruns 次数 失败用例重新执行多少次
-
'--reruns=5' 失败用例重新执行5次
-
-x 表示只要有一个用例报错,那么测试停止
-
--maxfail=次数 表示出现多少个报错就停止
-
-k 根据测试用例的部分字符串执行测试用例
-
-k='包含的字符串'
-
7.pytest的命令行执行方式
-
7.1 指定目录下的py文件执行
-
pytest.main(['-vs','./testcase'])
-
7.2指定py文件执行
-
pytest.main(['-vs','py文件'])
-
7.3通过nodeid指定用例执行,nodeid由模块名,分隔符,类名,方法名,函数名组成
-
(::双冒号为分隔符)
-
#目录下指定的测试用例(没有类)
-
pytest.main(['-vs','./testcase::test_01'])
-
#目录下指定的测试类下面的测试用例
-
pytest.main(['-vs','./testcase::Testpy::test_01'])
-
8.pytest.main()执行方法 单独的py文件中的pytest.main()
-
8.1用来读取pytest.ini文件
-
8.2可指定参数执行 pytest.main(['-vs','py文件'])
-
ps:该文件名最好不要以test开头,该文件最好与pytest.ini文件在同一项目路径下,与测试用例属于同一目录,会优先读取pytest.ini文件
-
9.conftest.py文件的作用及用法
-
9.1 conftest.py文件名字固定不能修改
-
9.2 congftest.py文件所在目录必须存在__init__.py文件
-
9.3 不能被其他模块导入
-
9.4 所有目录测试文件执行前都会执行一遍conftest.py文件
-
9.5 结合@pytest.fixture使用
-
9.5.1 fixture的作用范围
-
pytest.fixture(scope='function')
-
fixture里面的scope参数可以控制fixture的作用范围:session>module>class>functon
-
-function:每一个函数或方法都会调用(默认就是function)
-
-class:每一个类都会调用一次
-
-module:每一个.py文件都会调用一次
-
-session:多个文件调用一次,可以跨.py文件调用
-
9.5.2 使用场景
-
每个接口需要共用的token
-
每个接口需要共用到的测试用例数据(如参数化数据,数据驱动)
-
每个接口需要共用到的配置信息
-
======================================================================================================
-
pytest配置文件 pytest.ini
-
#可以改变pytest的默认规则,比如测试类以Test开头,测试用例以test_开头
-
#文件位置 一般放在项目的根目录下
-
#编码:必须是ansi,可以用notepad 转换
-
#作用:修改pytest默认的行为
-
#运行规则:不管是主函数模式还是命令行模式都会去读这个配置文件
-
[pytest]
-
#命令行的参数
-
addopts=-vs
-
#测试用例的路径,多个路径用空格隔开
-
testpaths=./testcase ./testcase2
-
#模块名的规则(可自定义)
-
python_files=test_*.py
-
#类名的规则 (可自定义)
-
python_classes=Test*
-
#方法名的规则 (可自定义)
-
python_functions=test
-
-
-
"""
-
-
class Test_pytest_1():
-
#将上一个方法里面的返回传递给下一个方法,调用的时候传入方法名
-
global a
-
a = 5
-
-
def test_1(self):
-
list = []
-
for i in range(1,a):
-
list.append(i)
-
return list
-
-
def test_2(self,get_data):
-
for res in get_data:
-
print(res)
-
-
-
-
# @pytest.mark.skip('我被无条件跳过')
-
# def test_b(self):
-
# print('我是order2')
-
# @pytest.mark.skipif(con == '冒烟', reason='我是skipif,因为条件成立被跳过了')
-
# def test_a(self):
-
# print('我是order1')
-
# @pytest.mark.skipif(con == '冒烟1', reason='我是skipif,因为条件不成立没有被跳过')
-
# @pytest.mark.xfail()
-
# def test_c(self):
-
# assert 1==2
-
-
-
# def setup_method(self):
-
# print('每个用例执行前我都会执行')
-
# def setup_class(self):
-
# print('我只执行一次,在用例执行前执行')
-
# def test1(self):
-
# print('我是第一条用例')
-
# def test2(self):
-
# print('我是第二条用例')
-
# def teardown_class(self):
-
# print('我只执行一次,在用例执行完后执行')
-
# def teardown_method(self):
-
# print('每个用例执行后我都会执行')
-
#单个参数化
-
# @pytest.mark.parametrize('a', (1, 2, 3, 4))
-
# def test_para(self,a):
-
# print(a)
-
#多个参数化
-
#如下3个参数,分别去对应的列表中的下标,如b取值完的结果1,4,7
-
# @pytest.mark.parametrize('b,c,d',([1,2,3],[4,5,6],[7,8,9]))
-
# def test_paramore(self,b,c,d):
-
# print(b,c,d)
-
-
-
-
-
if __name__ == '__main__':
-
pytest.main(['-vs','-r''test_pytest.py'])
pytest参数化打样
单个参数化,如下:会执行4次
多个参数化
初始化方法,结束方法
自定义测试用例执行顺序
下载插件pip install pytest-ordering
@pytest.mark.run(order=num)
ps:都要做标记才生效
跳过测试用例执行
@pytest.mark.skip(‘跳过的理由’)
@pytest.mark.skipif(con==条件,reason=条件成立时跳过)
Pytest.fixture()
单个参数传递
多个参数传递
定义一个全局变量和一个空列表,将需要返回的值追加到列表中然后return返回列表,下一个方法再遍历列表
allure的一些用法
allure安装教程(随便找了个,到时候百度一堆)https://www.cnblogs.com/hantongxue/p/14372291.html
要导入os模块,放在mian方法里面执行
ps:生成allure报告需在这里设置为unittest运行才行
-
# 执行pytest单元测试,生成 Allure 报告需要的数据存在 /temp 目录
-
pytest.main(['--alluredir', './temp'])
-
# 执行命令 allure generate ./temp -o ./report --clean ,生成测试报告
-
os.system('allure generate ./temp -o ./report --clean')
-
import pytest
-
import allure
-
"""
-
@allure.step
-
pytest支持使用@allure.step修饰某些测试用例中需要的函数,使测试用例在allure报告中能够更加详细的显示测试过程
-
@allure.step("输入的用户名")
-
@allure.attach
-
使用allure.attach可以给报告中添加文件,图片,log,html代码等
-
@allure.description
-
如果想在测试报告中展示测试用例的描述信息
-
@allure.description('这是测试用例的描述信息')
-
@allure.title
-
使用allure.title()可以重命名测试用例在allure报告中的名称
-
@allure.title('登录成功的场景')
-
@allure.link
-
@allure.testcase
-
@allure.issue
-
这三种特性都可以给测试用例添加一个链接
-
@allure.testcase('http:www.百度.com/','测试用例地址')
-
@allure.feature
-
@allure.story
-
feature和story被称为行为驱动标记,因为使用这两个标记,通过报告可以更加清楚的掌握每个测试用例的功能和每个测试用例的测试场景
-
@allure.feature('测试类用feature')
-
@allure.story('测试方法用story')
-
@allure.severity
-
此标记用来标识测试用例或者测试类的严重级别,分为blocker,critical,normal,minor,trivial 5个级别
-
"""
-
-
class Test_login():
-
-
#阻塞
-
def test_logina(self):
-
allure.attach("这是一个纯文本",name="文本信息",attachment_type=allure.attachment_type.TEXT) #添加文本
-
print("这是登录,用户名正确,登录成功")
-
pass
-
-
-
#严重
-
def test_loginb(self):
-
allure.attach("<body>这是一个网页</body>",name="HTML测试模块",attachment_type=allure.attachment_type.HTML) #添加网页
-
-
print("这是登录,密码正确,登录成功")
-
pass
-
-
-
# --allure-link-pattern=issue:https:www.badu.com
-
-
#正常问题
-
def test_loginc(self):
-
allure.attach.file("./picture/微信头像.jpg",name="这是一个图片",attachment_type=allure.attachment_type.JPG) #添加图片
-
print("这是登录,用户名错误,登录失败")
-
pass
-
-
-
-
#不太重要
-
def test_logind(self):
-
with allure.step("点击用户名输入框"):
-
print("输入用户名")
-
with allure.step("点击输入密码输入框"):
-
print("输入密码")
-
print("点击登录按钮")
-
with allure.step("点击登录后登录失败"):
-
assert "1" == 1
-
print("这是登录,密码错误,登录失败")
-
pass
-
-
Testcase_link = "https://www.百度.com"
-
-
-
#不重要
-
def test_logine(self):
-
print("这是登录,用户不存在,请重新注册")
-
pass
-
-
-
def test_loginf(self):
-
print("这是登录,密码已锁定,请重置密码")
-
pass
-
-
-
def test_loging(self):
-
print("这是登录,密码为空,请输入密码")
-
pass
-
-
if __name__ =='__main__':
-
pytest.main("-v -s")
pytest钩子函数的一些用法(直接用,不要记)
-
Pytest 测试报告相关钩子函数使用说明
-
-
1、在测试报告中追加测试执行人描述信息:
-
1.1 进入conftest.py文件(放在框架根目录下)
-
1.2 添加以下代码
-
from py.xml import html
-
import pytest
-
-
#*********************************************************
-
#在html测试报告中添加测试人信息
-
-
def pytest_html_results_summary(prefix, summary, postfix):
-
prefix.extend([html.p("测试人: 测试人姓名")])
-
-
执行测试报告,可以使用指令也可以使用main方法如下:
-
if __name__ == '__main__':
-
pytest.main(['XXX.py','--html=XXReport.html'])
-
-
2、在测试报告中追加每个测试方法的执行时间:
-
2.1 进入conftest.py
-
2.2 添加以下代码
-
#在html测试报告中添加测试执行时间
-
-
def pytest_html_results_table_row(report, cells):
-
cells.insert(1, html.td(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), class_='col-time'))
-
cells.pop()
-
-
3、在测试报告中添加错误截图:
-
3.1进入conftest.py
-
3.2 添加以下代码
-
#导入selenium类库
-
from selenium import webdriver
-
-
#定义全局的driver变量
-
driver = None
-
#设置一个browser固件
-
-
def browser():
-
global driver
-
if driver is None:
-
driver = webdriver.Chrome()
-
return driver
-
-
#定义截屏方法
-
def _capture_screenshot():
-
return driver.get_screenshot_as_base64()
-
-
#定义添加错误图片的钩子函数
-
-
def pytest_runtest_makereport(item):
-
"""
-
当测试失败的时候,自动截图,展示到html报告中
-
:param item:
-
"""
-
pytest_html = item.config.pluginmanager.getplugin('html')
-
outcome = yield
-
report = outcome.get_result()
-
extra = getattr(report, 'extra', [])
-
-
if report.when == 'call' or report.when == "setup":
-
xfail = hasattr(report, 'wasxfail')
-
if (report.skipped and xfail) or (report.failed and not xfail):
-
file_name = report.nodeid.replace("::", "_") ".png"
-
screen_img = _capture_screenshot()
-
if file_name:
-
html = '<div><img src="https://img-blog.csdnimg.cn/2022010622350215226.png" alt="screenshot" style="width:600px;height:300px;" ' \
-
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
-
extra.append(pytest_html.extras.html(html))
-
report.extra = extra
-
pytest 配置文件之 conftest.py
-
#随便搞的个,到时候用的时候详见@pytest.fixture()的用法
-
import pytest
-
import csv
-
@pytest.fixture(scope='session')
-
def get_data():
-
list_data=[]
-
file_row = open('ccssvv.csv', 'r')
-
table = csv.reader(file_row)
-
# 跳过首行读取
-
table.__next__()
-
# 逐行读取csv文件
-
for row in table:
-
list_data.append(row[0])
-
return list_data
conftest.py结合fixture
fixture conftest中返回的数据传到调用的方法中,调用方的传参即conftest中的方法名
pytest 配置文件之pytest.ini文件
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcffji
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13