一、简介
- 1.基于python的单元测试框架,它可以和selenium,requests,appium结合实现自动化测试;
- 2.实现用例跳过skip和reruns失败用例重跑;
- 3.它可以结合allure-pytest插件生成allure报告;
- 4.很方便和jenkins实现持续集成;
- 5.有很多强大的插件:
pytest-html(生成html报告的插件) pytest-xdist(多线程运行的插件) pytest-orderding(改变用例的执行顺序插件) pytest-rerunfailres(失败用例重跑的插件) allure-pytest(生成美观自定义的allure报告)
放到一个requirements.txt的文档中,如:
pytest
pytest-html
pytest-xdist
pytest-ordering
pytest-rerunfailures
allure-pytest
然后通过:pip install -r requirements.txt
二、常用断言
- 与unittest不同,pytest使用的是python自带的assert关键字来进行断言
- assert关键字后面可以接一个表达式,只要表达式的最终结果为True,那么断言通过,用例执行成功,否则用例执行失败
pytest 里面断言实际上就是 python 里面的 assert 断言方法,常用的有以下几种
assert xx :判断 xx 为真
assert not xx :判断 xx 不为真
assert a in b :判断 b 包含 a
assert a == b :判断 a 等于 b
assert a != b :判断 a 不等于 b
三、执行方式
(一)带参数
1.pytest
pytest ![在这里插入图片描述](https://img-blog.csdnimg.cn/8cbe8c16bbe44513abcae7ac085399a3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
2.pytest -vs
- -v:输出更加详细的信息。比如文件和用例名称等;
- -s:输出调试信息。打印信息等;
pytest -vs
![在这里插入图片描述](https://img-blog.csdnimg.cn/0a24012588c74a30bc3f306630233679.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
3.pytest -vs -n=2 多线程
pytest -vs -n=2
![在这里插入图片描述](https://img-blog.csdnimg.cn/d5eb95e136814116b286b6f6f2432aba.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
4.--reruns num 失败重跑(前提安装插件:pytest-rerunfailres)
pytest -vs --reruns=2
raise Exception() 抛出异常try except 解决异常 ![在这里插入图片描述](https://img-blog.csdnimg.cn/4a3648d38ad1462193d9a4b606e68caf.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
5.出现一个用例失败则停止测试;如:pytest -vs -x
![在这里插入图片描述](https://img-blog.csdnimg.cn/1dd85d49345b471f88e8a0e113ce7e39.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
6.–maxfail 出现N个失败才终止测试,如:pytest -vs --maxfail=2
![在这里插入图片描述](https://img-blog.csdnimg.cn/b345ebc734e34650922a9abb7496ed9a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
7.–html 生成html测试报告(前提安装插件:pytest-html )
如:pytest -vs --html ./reports/result.html ![在这里插入图片描述](https://img-blog.csdnimg.cn/c6170b8e0bd249c8a102af3b725a3475.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
8.-k 运行测试用例中包含某个字符串的测试用例;
pytest -vs -k "can"
![在这里插入图片描述](https://img-blog.csdnimg.cn/ab5d1e7a4fb140808bc7901c30f582a7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
(二)、通过主函数运行
import pytest
if __name__ == '__main__':
pytest.main(["-vs"])
![在这里插入图片描述](https://img-blog.csdnimg.cn/075ee952c70d4aaa942989daabdf9b13.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
(三)、指定模块运行
import pytest
if __name__ == '__main__':
pytest.main(["-vs","testcase/test_hello.py"])
![在这里插入图片描述](https://img-blog.csdnimg.cn/19be7785809d4dbaa05f4ec73367df25.png)
(四)、指定文件夹
import pytest
if __name__ == '__main__':
pytest.main(["-vs","testcase/"])
![在这里插入图片描述](https://img-blog.csdnimg.cn/2e991b7f000a4685942d374eae73a0d4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
四、通过全局配置文件pytest.ini文件执行
(不管是命令行还是主函数都会读取这个配置文件)
注意:
- 一般放在项目的根目录下,名称必须是pytest.ini
- 编码格式为ANSI(当有中文时可能需要改变编码格式为GB2312
- pytest.ini文件可以改变默认的测试用例规则
- 不管是命令行运行还是主函数运行,都会加载运行这个配置文件
import pytest
class TestDemo:
@pytest.mark.smoke
def test_beijing(self):
print("01")
def test_cheng(self):
print("02")
@pytest.mark.smoke
def test_03(self):
print("03")
[pytest]
addopts = -vs -m "smoke"
testpaths=./demo
python_files=test_*.py
python_classes=Test*
python_functions=test_*
markers=
smoke:冒烟用例
product_manger:商品管理
-m "smoke"指的是只执行冒烟测试用例
注意:文件中最好不要出现中文,如果有中文的情况下,比如使用notpad++改成GBK的编码。
![在这里插入图片描述](https://img-blog.csdnimg.cn/9af79f2f73a24442b91be8d2d56ffc14.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
五、pytest跳过测试用例
import pytest
class TestDemo:
@pytest.mark.skip(reason="无理由条件跳过")
def test_beijing(self):
print("01")
def test_cheng(self):
print("02")
def test_03(self):
print("03")
![在这里插入图片描述](https://img-blog.csdnimg.cn/d554ea29473e46e18eb8bfd293a62b5a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
@pytest.mark.skipif(age<10,reason="年龄<10")
![在这里插入图片描述](https://img-blog.csdnimg.cn/8b7c6a42295a4057855c6392ea78b2f3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGVyX0NoZW5n,size_20,color_FFFFFF,t_70,g_se,x_16)
|