1、使用pytest运行unittest编写的测试用例
pytest可以完美的兼容unittest框架,这也是其功能强大之处之一。
项目地址:unittest-demo
项目结构如下,其中test_register.py是注册功能测试用例,test_login.py是登录功能测试用例。
Terminal窗口,项目根目录中,命令行模式输入:pytest -v
运行结果可知,pytest命令也可以运行unittest编写的测试用例。
2、兼容pytest.ini文件
项目根目录中,新建主配置文件pytest.ini如下:
[pytest]
addopts = -vs
testpaths = testcases
python_files = test_register.py test_login.py
Terminal窗口,项目根目录中,命令行模式输入:pytest
运行结果可知,pytest.ini文件中的配置选项生效了,兼容pytest.ini文件。
3、兼容conftest.py文件
testcases测试用例目录中,新建夹具函数定义文件conftest.py如下:
import pytest
@pytest.fixture(scope="session", autouse=True)
def browser():
print("打开浏览器")
yield True
print("关闭浏览器")
Terminal窗口,项目根目录中,命令行模式输入:pytest
运行结果可知,conftest.py中定义的session级别夹具生效了,兼容conftest.py文件。
4、兼容mark标记
修改注册功能测试用例文件test_register.py,标记test02测试用例逾期失败,修改后的文件如下:
"""
注册功能测试用例
"""
import unittest
import pytest
class TestRegister(unittest.TestCase):
def setUp(self) -> None:
print('setUp:打开注册页面')
def tearDown(self) -> None:
print('tearDown:关闭注册页面')
def test01(self):
print('test01:注册成功用例')
@pytest.mark.xfail
def test02(self):
print('test02:注册失败用例')
assert 1 == 2
Terminal窗口,项目根目录中,命令行模式输入:pytest
运行结果可知,使用pytest.mark标记的测试用例生效了,兼容mark标记。
总结:pytest完美的兼容了unittest框架。
reference:
How to use unittest-based tests with pytest — pytest documentation
Original: https://blog.csdn.net/panc_guizaijianchi/article/details/126486480
Author: 笃行之.kiss
Title: pytest合集(12)— 兼容unittest框架
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/775660/
转载文章受原作者版权保护。转载请注明原作者出处!