Pytest + Allure + Jenkins
目录
=========pytest 核心组件fixture========
使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例
fixture自动使用autouse=True与作用域scope
1,pytest测试框架
(1)单元测试:对软件中最小的单元(如函数‘,模块)进行测试
(2)框架:规范,帮助进行用例管理
(3)自动化框架:app自动化------appium
web自动化--------selenium
接口自动化--------request
具体使用
-
import pytest
-
class Testcase:
-
def setup(self):
-
print('start: --------loading-------- ')
-
-
def teardown(self):
-
print('end:------------close--------')
-
-
def test_01(self):
-
print("the first test")
-
assert 1 == 3
-
-
# 用例都是从上往下依次执行,若想先执行这个,可以用mark设置
-
-
def test_02(self):
-
print("the second test")
-
assert 1 == 1
-
-
if __name__ == '__main__':
-
# ['-s','-v','test_py.py']
-
# -s 显示结果 -v显示详细用例
-
pytest.main()
配置pytest.ini文件可以改变命名规则:
-
[pytest]
-
python_files=test_*.py *_test.py #文件都是以test_开头 或者 _test结尾命名,不符合则检测不到
-
python_classes=Test*
-
python_functions=test_*
安装allure pytest(pip)
Unittest VS pytest
pytest安装&规则&约束
testpath在ini文件里面定义的测试路径。norecursedirs定义不想找的目录
在pytest.ini中定义:(pytest,ini中不能再注释中写中文,会报错!!)
-
[pytest]
-
; 只运行指定路径testpaths下文件(该行中文不可以写)
-
testpaths = testing doc
-
; 不运行该指定目录
-
norecursedirs = doc*
pytest断言
电商网站登录
path里路径 r 表示:后面的r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"中的反斜杠“\”不存在转义,就是原始的路径
-
from _pytest.monkeypatch import derive_importpath
-
import pytest
-
from selenium import webdriver
-
from time import sleep
-
-
def test_login_success():
-
driver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
-
driver = webdriver.Chrome(executable_path=driver_path)
-
driver.get("http://39.98.138.157/shopxo")
-
driver.find_element_by_link_text("登录").click()
-
driver.find_element_by_xpath("//*[@name='accounts']").send_keys("1111111111")
-
driver.find_element_by_xpath('//*[@name="pwd"]').send_keys("1111111111")
-
driver.find_element_by_xpath("//button[text()='登录']").click()
-
sleep(2)
-
# test equall?
-
#
-
welcome = driver.find_element_by_xpath("//*[contains(text(),'欢迎来到')]").text
-
-
# assert '1111111111, 欢迎来到' == welcome
-
-
driver.quit()
-
-
if __name__ == '__main__':
-
pytest.main(["-s","test_cema_assert.py"])
断言成功:
常用断言方法:![学新通 学新通](/static/whole/images/alert/data-load/load-15.gif)
pytest参数化
-
import pytest
-
from selenium import webdriver
-
from time import sleep
-
-
-
"user,pw,excepted",
-
[("1111111111","1111111111","1111111111,欢迎来到"),
-
("dsgfas","1","1111111111,欢迎来到")],
-
ids=["case1","case2"])
-
#以上是两组用例:case1和case2
-
def test_login(user,pw,excepted):
-
driver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
-
driver = webdriver.Chrome(executable_path=driver_path)
-
driver.get("http://39.98.138.157/shopxo")
-
driver.find_element_by_link_text("登录").click()
-
driver.find_element_by_xpath("//*[@name='accounts']").send_keys(user)
-
driver.find_element_by_xpath('//*[@name="pwd"]').send_keys(pw)
-
driver.find_element_by_xpath("//button[text()='登录']").click()
-
sleep(2)
-
# test equall?
-
#
-
welcome = driver.find_element_by_xpath("//*[contains(text(),'欢迎来到')]").text
-
-
assert excepted == welcome
-
-
driver.quit()
-
-
if __name__ == '__main__':
-
pytest.main(["-s","test_param.py"])
pytest常用运行参数
pytest生成测试报告
(1)生成junitXML文件(在指定文件夹report中)
(2)生成在线报告
pytest控制测试用例执行
-
import pytest
-
-
def test_fail01():
-
print("第一次失败")
-
assert 1==2
-
-
def test_fail02():
-
print("第二次失败")
-
assert 1==2
-
-
def test_fail03():
-
print("第三次成功")
-
assert 1==1
-
-
if __name__ == '__main':
-
pytest.main(["--maxfail=2","test_control.py"])
-
-
-
多进程运行用例
安装pytest-xdist(用pip)
将测试发送到2个CPU
pytest.main(["-n","2","test_many.py"])
使用与计算机具有的CPU内核一样多的进程
pytest.main(["-n","auto","test_many.py"])
-
import py
-
import pytest
-
-
def test_case01():
-
assert 1==1
-
-
def test_case02():
-
assert 1==1
-
-
def test_case03():
-
assert 1==12
-
-
def test_case04():
-
assert 1==3
-
-
def test_case05():
-
assert 1==12
-
-
def test_case06():
-
assert 1==1
-
-
if __name__ == '__main__':
-
# 将测试发送到2个CPU
-
# pytest.main(["-n","2","test_many.py"])
-
# 使用与计算机具有的CPU内核一样多的进程
-
pytest.main(["-n","auto","test_many.py"])
-
通过标记表达式执行用例
pytest -m slow
这条命令会执行被装饰器@pytest.mark.slow装饰的所有测试用例
配置pytest.ini文件:
-
[pytest]
-
markers =
-
slow: marks tests as slow(deselect with '-m "not slow"')
-
serial
前面表示会执行mark的用例,括号内是不选中标记的用例:则只会执行后两条
-
import pytest
-
-
def test_fail01():
-
print("第一次失败")
-
assert 1==2
-
-
-
def test_fail02():
-
print("第二次失败")
-
assert 1==2
-
-
-
def test_fail03():
-
print("第三次成功")
-
assert 1==1
-
-
if __name__ == '__main':
-
# pytest.main(["--maxfail=2","test_control.py"])
-
# 通过标记表达式执行
-
pytest.main(["-m","slow","test_mark.py"])
-
重新运行失败用例
pytest的类外 setup & teardown函数
-
import pytest
-
-
def multiply(a,b):
-
return a * b
-
-
def setup_module(module):
-
print("setup_module=========================>")
-
-
def teardown_module(module):
-
print("teardown_module========================>")
-
-
def setup_function(function):
-
print("setup_function--------------------->")
-
-
def teardown_function(function):
-
print("teardown_dunction----------------->")
-
-
def setup():
-
print("setup_module====================>")
-
-
def setup():
-
print("setup----->")
-
-
def teardown():
-
print("teardown--->")
-
-
def test_mul_01():
-
print('test_3_4')
-
assert multiply(3,4) == 12
-
-
def test_mul_02():
-
print('test_6_8')
-
assert multiply(6,8) == 48
-
-
if __name__ == '__main__':
-
pytest.main(["-s","test_setup01.py"])
类之外的执行顺序:module ---> function ---> setup
类内的setup和teardown函数:
=========pytest 核心组件fixture========
-
import pytest
-
-
-
def first_fix():
-
return ["a"]
-
-
def test_str(first_fix):
-
# 测试执行
-
first_fix.append("b")
-
# 断言
-
assert first_fix == ["a", "b"]
-
print(first_fix)
-
-
if __name__ == '__main__':
-
pytest.main(["-s","-v"])
原生实现(不使用fixture):
-
def first_var():
-
return ["a"]
-
-
def test_str(varA):
-
varA.append("b")
-
assert varA == ["a", "b"]
-
print(varA)
-
-
entry = first_var()
-
test_str(entry)
fixture调用fixture
-
import pytest
-
-
-
def first_entry():
-
return "a"
-
-
-
def order(first_entry):
-
return [first_entry]
-
-
def test_string(order):
-
order.append("b")
-
-
assert order == ["a","b"]
-
print(order)
-
-
if __name__ == '__main__':
-
pytest.main(["-s"])
使用多个fixture
分开的用法:
使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例
都指定使用fixture1,所以fixture2未用!
叠加使用usefixtures
conftest.py-------测试配置文件
配置文件:
fixture自动使用autouse=True与作用域scope
class也执行在类外 方法,function每一个方法都执行
fixture之params参数化
执行1,2,3,次
跳过测试函数
无条件跳过测试函数
只执行用例2,其余两个跳过
自定义skip条件:
pytest.skip()方法跳过函数
只执行3
跳过测试类
只执行1
跳过模块
跳过所有用例
单个/多个参数
单参数:
多参数:
使用函数返回值形式传入参数值
parametrize参数中ids用法
方法一:
方法二:
parametrize叠加使用
当出现叠加使用和不叠加使用同时存在,优先使用叠加使用!ids也会叠加,叠加后,ids显示顺序是下面的叠加在上面的前面,而参数执行顺序是(a=11,b=21,c=31)(a=12,b=21,c=31).......
修改python traceback的输出
执行失败跳转pdb
Allure测试报告
Jenkins的CI和CD
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhhkbgfi
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22