目录
1,pytest测试框架
1.2,具体使用
安装allure+pytest(pip)
Unittest VS pytest
?pytest安装&规则&约束
pytest断言
? 常用断言方法:?
pytest参数化
pytest常用运行参数
?pytest生成测试报告
pytest控制测试用例执行
?多进程运行用例
通过标记表达式执行用例
?重新运行失败用例
?pytest的setup & teardown函数
![](https://img-blog.csdnimg.cn/22ee0ba619b04f3398572545836b3304.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/41f6f3bfddcc40658ac7eabf5c16974c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/05284ed642ed4c7ea5cba59663d6818f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/5b53cd27fe584b22869c8ce91b1a1327.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/c4dfd57154c4471fac6da1f45c3b3fe3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/72c9484c70594a408a14084dde58ac7d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/650f3379ebe44af7a8d0cb9ebe588b59.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/4941d41d416f44d19dba38b200136ea9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
1,pytest测试框架
(1)单元测试:对软件中最小的单元(如函数‘,模块)进行测试
(2)框架:规范,帮助进行用例管理
(3)自动化框架:app自动化------appium
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?web自动化--------selenium
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 接口自动化--------request
1.2,具体使用
![](https://img-blog.csdnimg.cn/fdfeb590c79845b2b77e57ec693eb27e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
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设置
@pytest.mark.run(order=1)
def test_02(self):
print("the second test")
assert 1 == 1
if __name__ == '__main__':
# ['-s','-v','test_py.py']
# -s 显示结果 -v显示详细用例
pytest.main()
![](https://img-blog.csdnimg.cn/9ec66d277ebd48edbf50453e5edaabdf.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16) ![](https://img-blog.csdnimg.cn/aa3d1937605046db89f57faf4d8fbdb3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?配置pytest.ini文件可以改变命名规则:
[pytest]
python_files=test_*.py *_test.py #文件都是以test_开头 或者 _test结尾命名,不符合则检测不到
python_classes=Test*
python_functions=test_*
安装allure+pytest(pip)
Unittest VS pytest
![](https://img-blog.csdnimg.cn/137e1e1fb0ad4cf7a26381b23cf49c1f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/e3342a569dae4f7895db9bf73a0a4012.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/efa1007493af47db9ef4bde02381f2c7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/e65dd9f9b25c40bb8161b9237b6a06b9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?pytest安装&规则&约束
![](https://img-blog.csdnimg.cn/f2656ea961864efcab6e0ce43ec0d28c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
? ?![](https://img-blog.csdnimg.cn/4075c43935d84266a15503ab0ef15cf3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?testpath在ini文件里面定义的测试路径。norecursedirs定义不想找的目录![](https://img-blog.csdnimg.cn/6177a2994b9e4e41b87592415be8b999.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
在pytest.ini中定义:(pytest,ini中不能再注释中写中文,会报错!!)
[pytest]
; 只运行指定路径testpaths下文件(该行中文不可以写)
testpaths = testing doc
; 不运行该指定目录
norecursedirs = doc*
pytest断言
电商网站登录
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"])
断言成功:![](https://img-blog.csdnimg.cn/2aaef740dda5455ea562f03b42b7abd1.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
? 常用断言方法:![](https://img-blog.csdnimg.cn/b4a70a62ac9b425380652451a2ebf024.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_10,color_FFFFFF,t_70,g_se,x_16)
pytest参数化
![](https://img-blog.csdnimg.cn/2ceb76ec238241c885acae30ece25666.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
import pytest
from selenium import webdriver
from time import sleep
@pytest.mark.parametrize(
"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"])
![](https://img-blog.csdnimg.cn/e03c825b4b9c4190adcff0e159426372.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
pytest常用运行参数
?![](https://img-blog.csdnimg.cn/ccc73310f0e0436087e2f8551576d3b5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_18,color_FFFFFF,t_70,g_se,x_16) ![](https://img-blog.csdnimg.cn/cda6c11b52cb4f25afcbf078dc3f238d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/f115f5f7e4244d228f314766471774bc.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/eda323b3d07e42a8aecfddebf0301fea.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?pytest生成测试报告
(1)生成junitXML文件(在指定文件夹report中)
![](https://img-blog.csdnimg.cn/9f14e9577f5947d5998a62301f536eb7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/1775cf4b64634df88b9cbb8480a1fb39.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_14,color_FFFFFF,t_70,g_se,x_16)
(2)生成在线报告
![](https://img-blog.csdnimg.cn/0fbbae5751004ce19ab8d8faace744cc.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
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"])
![](https://img-blog.csdnimg.cn/e48c52c2be4049f5969a0219464aba2b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?多进程运行用例
安装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"])
?![](https://img-blog.csdnimg.cn/cff9c7ab05ad44e4bd289db6a40b1dbb.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
通过标记表达式执行用例
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
@pytest.mark.slow
def test_fail02():
print("第二次失败")
assert 1==2
@pytest.mark.slow
def test_fail03():
print("第三次成功")
assert 1==1
if __name__ == '__main':
# pytest.main(["--maxfail=2","test_control.py"])
# 通过标记表达式执行
pytest.main(["-m","slow","test_mark.py"])
?重新运行失败用例
![](https://img-blog.csdnimg.cn/e1f7863abf1f4f4183054f1e172bb60c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/2588c89ca1cb4bf7bd454d3be6f87d22.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_15,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/5ffa5b65109a4eb9abdc77aedc24016e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/7834e5c49c0847c98130506f1f0d7581.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?![](https://img-blog.csdnimg.cn/fdfcc9a837274b99af2af24903050d4d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YWt57Gz5LiA,size_20,color_FFFFFF,t_70,g_se,x_16)
?pytest的setup & teardown函数
|