一、fixtrue是什么?
pytest特有的功能;
能自定义的完成测试方法执行之前配置资源(测试初始化);
能自定义的完成测试方法执行之后释放资源(测试环境清理);
可以进行参数化测试;
在代码中写在函数前面,长这个样子:@pytest.fixture() 。
二、简单使用
· 2.1 建立一个 test_demo_01.py 文件,包含测试初始化的代码:
import pytest @pytest.fixture() def setUp(): #方法名任意 print('setUp') #测试初始化语句 def testcase(setUp): print('exectue testcase01') assert 1 if __name__=='__main__':
pytest.main(["-s"]) #-s 为了能看见print的输出效果
· 2.2 建立一个 test_demo_02.py 文件,包含测试初始化和测试环境清理的代码:
import pytest @pytest.fixture() def setUp(): #方法名任意 print('setUp') #测试初始化语句 yield #测试环境清理语句 print('tearDown') def testcase(setUp): print('exectue testcase01') assert 1 if __name__=='__main__':
pytest.main(["-s"]) #-s 为了能看见print的输出效果
上述2个实例小结:
· 调用初始化的时候,不要漏掉在测试方法中增加 fixture下的函数名:如 def testcase(setUp):
· yield下的语句为测试环境清理语句
· 测试初始化简单点理解就是在执行测试方法之前要执行的代码
· 测试环境清理简单点理解就是在执行测试方法之后要执行的代码
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.