pytest模块的使用
pytest模块的使用
pytest是第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效。
安装pytest模块使用
pip install pytest
安装好之后,到cmd中输入
pytest --version
检查是否安装成功。
pytest运行方法
想要用pytest运行,首先要导入模块
import pytest
比如创建一个demo.py文件,内容为:
"""
@File : test_demo.py.py
@Author : sary
@Time : 2021/4/13 13:50
@explain :
"""
# -*- coding:utf-8 -*-
import pytest # 导入包
def test_sucess(): # 定义第一个测试用例,assert 1表示断言成功
print("test sucess")
assert 1
def test_fail(): # 定义第二个测试用例
print("test fail")
assert 0
if __name__ == "__main__":
pytest.main(['-s','test_demo.py'])
运行结果:
D:\Python39\python.exe "D:\PyCharm 5.0.3\helpers\pycharm\pytestrunner.py" -p pytest_teamcity D:/pycharm_projects/pytest_study/test_demo.py
Testing started at 下午 01:57 ...
============================= test session starts =============================
platform win32 -- Python 3.9.0, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\
collected 2 items
..\..\..\..\pycharm_projects\pytest_study\test_demo.py test sucess
.test fail
F
def test_fail(): # 定义第二个测试用例
print("test fail")
> assert 0
E assert 0
D:\pycharm_projects\pytest_study\test_demo.py:19: AssertionError
================================== FAILURES ===================================
__________________________________ test_fail __________________________________
def test_fail(): # 定义第二个测试用例
print("test fail")
> assert 0
E assert 0
D:\pycharm_projects\pytest_study\test_demo.py:19: AssertionError
=========================== short test summary info ===========================
FAILED ..\..\..\..\pycharm_projects\pytest_study\test_demo.py::test_fail - as...
========================= 1 failed, 1 passed in 0.16s =========================
上述输出中:
test_demo.py
test sucess . test fail F
F:表示测试失败
.表示测试成功
这里要提醒一点,pytest必须遵循以下规则:
1、测试文件名必须以“test_”开头或者以”_test”结尾
2、测试方法必须以“test_”开头
3、测试类命名以Test开头
那么执行“pytest -s test_demo.py”这句话的时候,python就会自动去寻找test_开头或者结尾的函数来执行。
setup()方法teardown()方法
setup()方法和teardown()方法是两个特殊方法,setup()是在每一个编写的测试用例执行前必然会执行的方法,teardown()方法是在每个测试用例执行后执行的方法。
比如上面的两个测试用例,修改为:
"""
@File : test_demo.py.py
@Author : sary
@Time : 2021/4/13 13:50
@explain :
"""
# -*- coding:utf-8 -*-
import pytest # 导入包
class TestLogin:
def setup(self):
print("setup")
def teardown(self):
print("teardown")
def test_sucess(self):
print("test sucess")
def test_fail(self):
print("test fail")
if __name__ == "__main__":
pytest.main(['-s','test_demo.py'])
输出:
setup test sucess .teardown setup test fail .teardown
setup_class()、teardown_class()方法
如果理解了上面讲解的setup和teardown方法,那么setup_class和teardown_class方法也容易理解,这两个方法是针对测试类的,是每一个类执行前后必须执行的方法
"""
@File : test_demo.py.py
@Author : sary
@Time : 2021/4/13 13:50
@explain :
"""
# -*- coding:utf-8 -*-
import pytest # 导入包
class TestLogin:
def setup_class(self):
print("setup_class")
def teardown_class(self):
print("teardown_class")
def setup(self):
print("setup")
def teardown(self):
print("teardown")
def test_sucess(self):
print("test sucess")
def test_fail(self):
print("test fail")
if __name__ == "__main__":
pytest.main(['-s','test_demo.py'])
运行结果:
setup_class setup test sucess .teardown setup test fail .teardown teardown_class
pytest配置文件
pytest运行时可以有一个配置文件,名字为pytest.ini,并且这个配置文件名字必须这么写,一般放到测试项目目录中。

pytest.ini的内容为:
[pytest] # 参数 addopts = -s # 搜索哪个文件夹 testpaths = ./scripts # 搜索的文件名以test_开头 python_files = test_*.py # 搜索的类名以Test开头 python_classes = Test* # 搜索的方法名以test_开头 python_functions = test_*
有了这个配置文件,就等同于告诉了python要运行哪个文件夹下的哪些文件。到cmd中运行的时候,进入到你自己的项目路径,不需要写参数,直接输入pytest就可以,就会按照你的配置文件来运行该运行的用例。
pytest常用插件
生成测试报告
pytest-html
安装方式:
pip install pytest-html
安装成功后,运行的方式,有两种,第一种:
在终端从
pytest
改成
pytest --html=../report/report.html
意思是创建一个文件夹为report,在report文件夹中创建一个html文件,该文件里面存的就是运行的测试用例结果

报告独立显示
上面方法生成的报告,css是独立的,分享报告的时候样式会丢失,为了更好的分享发邮件展示报告,可以把css样式合并到html里
$ pytest --html=report.html --self-contained-html
第二种运行方式:
修改pytest.ini配置文件:
addopts = -s --html=report/report.html
然后在终端依旧输入pytest运行,出来的结果是一样的,推荐第二种方式,更简单。
addopts还有很多其他参数
pytest.ini的作用
可以改变pytest的运行方式,读取配置信息,并按指定的方式去运行
非test文件
pytest里面有些文件是非test文件
- pytest.ini:pytest的主配置文件,可以改变pytest的默认行为
- conftest.py:测试用例的一些fixture配置
- init.py:识别该文件夹为python的package包
pytest.ini应该放哪里?
必须放在项目根目录下 ,不要乱放、乱起其他名字
浙公网安备 33010602011771号