一. 为什么要使用Pytest
官网:pytest: helps you write better programs — pytest documentation
Pytest操作简单易上手Pytest可以实现接口自动化、web自动化以及app自动化Pytest可以自动生成测试报告Pytest可以和jenkins持续集成Pytest包含很多功能强大的插件
二. 快速开始
1. 安装
pip install pytest
pytest --version
2.运行
class TestClass:
def test_1(self):
s = 'hello'
assert 'o' in s
def test_2(self):
i = 1
j = 3
assert i == j
注意:如果使用Pycharm运行文件,需要将默认运行方式改为pytest。
直接执行pytest命令看看会发生什么?
如果直接执行pytest,会查找当前目录及其子目录下所有为test_*.py或者*_test.py的文件,然后执行文件中所有以test开头的函数。
如果只需要执行其中一个函数呢?
3. 用例命名规则
文件命名为test_*.py或者*_test.py测试类以Test开头,并且不能有init方法测试方法以test开头所有的包 package 都包含__init__.py
4. 用例执行
执行某个目录下所有用例
pytest
执行某目录下某个测试类中的测试方法
pytest testcase/test_hello.py::TestClass::test_1
显示测试用例中print打印的内容
pytest -s
显示更详细的信息(一般直接使用-sv)
pytest -v
失败用例重构nums次
pytest --reruns nums
多线程执行测试用例
pytest -n nums
出现n个失败用例就停止
pytest --maxfail=n
执行用例名称包含hello的所有用例
pytest -sv -k 'hello'
执行用例名称不包含hello的所有用例
pytest -s -k "not hello"
执行用例名称包含hello或者word的所有用例
pytest -s -k "hello or word"
参考阅读
发表评论