pytest 测试框架之 fixture 进阶
简介
fixture 即测试用例执行的环境准备和消理,fixture 主要的目的是为了提供一种可靠和可重复性的手段去 运行那些最基本的测试内容。比如测试网站的功能时,每个测试用例都要登录和登出,利用 fixture 就可 以只做一次,否则每个测试用例都要做这两步。
fixture 类似于 setup/teardown 机制,但 fixture 更灵活的完成测试的初始化以及资源清理工作。可以使 用它将你需要执行 setup 的测试用例传入 fixture 函数名,不需要的测试用例可以不用传递,不传递 fixture 名的测试用例将不会执行 fixture 函数。它允许我们将复杂的测试需求归纳成更简单、更有组织的 测试函数。 @pytest.fixture 与 @pytest.mark.parametrize 结合实现参数化
如果测试数据需要在 fixture 方法中使用,同时也需要在测试用例中使用,可以在使用 parametrize 的时 候添加一个参数 indirect=True ,pytest 可以实现将参数传入到 fixture 方法中,也可以在当前的测试 用例中使用。
当 indirect 参数设置这个参数为 True 时,pytest 会把 argnames 当作函数去执行,将 argvalues 作 为参数传入到 argnames 这个函数里。创建“test_param.py”文件,代码如下:
 # 方法名作为参数test_user_data = ['Tome', 'Jerry']@pytest.fixture(scope="module")def login_r(request): # 通过request.param获取参数 user = request.param print(f"\n 登录用户:{user}") return user@pytest.mark.parametrize("login_r", test_user_data,indirect=True)def test_login(login_r): a = login_r print(f"测试用例中login的返回值; {a}") assert a != ""
   复制代码
 运行结果:
 test_mark_paramize.py::test_login[Tome]test_mark_paramize.py::test_login[Jerry]============================== 2 passed in\0.02s ===============================Process finished with exit code 0登录用户:Tome PASSED [ 50%]测试用例中login的返回值; Tome登录用户:Jerry PASSED [100%]测试用例中login的返回值; Jerry
   复制代码
 上面的结果可以看出,当 indirect=True 时,会将 login_r 作为参数,test_user_data 被当作参数 传入到 login_r 方法中,生成多条测试用例。通过 return 将结果返回,当调用 login_r 可以获取 到 login_r 这个方法的返回数据。
fixture 间相互调用
fixture 可以作为参数传递给其它的 fixture 函数。
工作中可能不同的功能,依赖的数据,环境不一样,需要定义多个功能函数。
比如:登录 fixture 函数中需要信赖连接数据库的 fixture 函数。可以将这些功能函数都定义为 fixture, 然后通过传递 fixture 函数,实现功能之间的数据交换。
 # contents of test_append.pyimport pytest@pytest.fixturedef connectDB(): return "name,school"@pytest.fixturedef login(connectDB): return [connectDB]def test_string(login): pass
   复制代码
 测试用例/fixture 可以请求多个 fixture
下面是一个示例:
 # contents of test_append.pyimport pytest# Arrange@pytest.fixturedef first_entry(): return "a"# Arrange@pytest.fixturedef second_entry(): return 2# Arrange@pytest.fixturedef order(first_entry, second_entry): return [first_entry, second_entry]# Arrange@pytest.fixturedef expected_list(): return ["a", 2, 3.0]def test_string(order, expected_list): # Act order.append(3.0) # Assert assert order == expected_list
   复制代码
 Fixture 终结器(Teardown/Cleanup)
添加终结器
但如果 yield 之前的内容发生异常,将不会执行 yield 后面的内容。
对于这种情况,还可以直接向测试的 request 对象添加 “finalizer” 终结器函数实现这个功能(与 yield fixtures 类似的结果,但能力更强)。
举个应用场景:
比如我们在读取文件的时候,需要打开文件,获取文件流对象,定义一个关闭文件的方法 close() , 在终结器中实现关闭操作。最后即使后面的代码有异常,也会执行终结器里面的操作。
 @pytest.fixture(scope='module', autouse=True)def operate_file(request): print("打开文件") fs = open('./datas/data.yaml', mode="r") def close(): print("关闭文件") fs.close() request.addfinalizer(close) datas = yaml.safe_load(fs) raise ValueError print(datas)
   复制代码
 搜索微信公众号:TestingStidio 霍格沃兹的干货都很硬核
评论