前言
最近帮测试同事在本地搭建一个测试框架pytest。于是总结了下安装使用步骤,记个笔记。
正文
放上快速上手的安装教程:
第一步:
pip3 install pytest
第二步:
pip3 install pytest-cov
第三步:
新建python脚本test_code.py
代码如下:
import pytest
@pytest.fixture(scope='function')
def setup_function(request):
def teardown_function():
print("teardown_function called.")
request.addfinalizer(teardown_function) # 此内嵌函数做teardown工作
print('setup_function called.')
@pytest.fixture(scope='module')
def setup_module(request):
def teardown_module():
print("teardown_module called.")
request.addfinalizer(teardown_module)
print('setup_module called.')
def test_1(setup_function):
print('Test_1 called.')
def test_2():
print("test_2")
然后运行命令行:
pytest --cov-report=html --cov=./ test_code.py
按照前面这几步其实就可以用了。效果如下:
后记
由于我这边是短时间快速集成,所以总结的比较简单。pytest框架其实还有很多可以深究的地方,后面有机会可以多更几篇博文,深入探讨一下。(写到这我发现好像我埋了很多坑比如算法导论、排序算法、编程语言,,,,溜了溜了:)