领取资料,咨询答疑,请➕wei: June__Go
上一小节我们学习了fixture的yield关键字,本小节我们讲解一下使用多个fixture的方法。
使用多个fixture
如果用例需要用到多个fixture的返回数据,fixture也可以return一个元组、list或字典,然后从里面取出对应数据。
#test_demo.py
import pytest
@pytest.fixture()
def user():
print("获取用户名")
a = "yoyo"
b = "123456"
return (a, b)
def test_1(user):
u = user[0]
p = user[1]
print("测试账号:%s, 密码:%s" % (u, p))
assert u == "yoyo"
if __name__ == "__main__":
pytest.main(["-s", "test_demo.py"])
当然也可以分开定义成多个fixture,然后test_用例传多个fixture参数
# test_demo.py
import pytest
@pytest.fixture()
def user():
print("获取用户名")
a = "yoyo"
return a
@pytest.fixture()
def psw():
print("获取密码")
b = "123456"
return b
def test_1(user, psw):
'''传多个fixture'''
print("测试账号:%s, 密码:%s" % (user, psw))
assert user == "yoyo"
if __name__ == "__main__":
pytest.main(["-s", "test_demo.py"])
fixture与fixture互相调用
#test_demo.py
import pytest
@pytest.fixture()
def first():
print("获取用户名")
a = "yoyo"
return a
@pytest.fixture()
def sencond(first):
'''psw调用user fixture'''
a = first
b = "123456"
return (a, b)
def test_1(sencond):
'''用例传fixture'''
print("测试账号:%s, 密码:%s" % (sencond[0], sencond[1]))
assert sencond[0] == "yoyo"
if __name__ == "__main__":
pytest.main(["-s", "test_demo.py"])
fixture的重命名
通过前面学习fixture可以正常的代替setup和teardown,我们在后期框架中的文件名过多,或者函数名过多,有时会和fixture名字进行重名,或者fixture调用容易出错。fixture也可以进行重新自己更改自己的名称。
在fixture源码中我们可以通过修改name的参数来更改fixture的名称。默认是我们定义def的名称
def fixture( # noqa: F811
fixture_function: Optional[_FixtureFunction] = None,
*,
scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
params: Optional[Iterable[object]] = None,
autouse: bool = False,
ids: Optional[
Union[
Iterable[Union[None, str, float, int, bool]],
Callable[[Any], Optional[object]],
]
] = None,
name: Optional[str] = None
说这么多,大家也不知道说的啥,举个例子吧,这里我把函数名为login的fixture通过配置参数name=“mitu_login”
# test_demo.py
import pytest
@pytest.fixture(name='mitu_login')
def login():
print('输入账号,输入密码')
print('完成登录功能!!!!')
yield
print('---退出登录---')
class Test_Login:
def test_01(self, mitu_login):
print('------用例01------')
def test_02(self):
print('------用例02------')
def test_03(self, mitu_login):
print('------用例03------')
if __name__ == '__main__':
pytest.main(['-s', 'test_demo.py'])
运行结果:
========================================================================================= test session starts ==========================================================================================
platform win32 -- Python 3.9.12, pytest-7.3.1, pluggy-0.13.1
rootdir: D:\PycharmProjects\Source_Code\pytest_demo
plugins: allure-pytest-2.13.2, html-4.1.1, metadata-3.0.0
collected 3 items
test_demo.py
SETUP F mitu_login
test_demo.py::Test_Login::test_01 (fixtures used: mitu_login).
TEARDOWN F mitu_login
test_demo.py::Test_Login::test_02.
SETUP F mitu_login
test_demo.py::Test_Login::test_03 (fixtures used: mitu_login).
TEARDOWN F mitu_login
========================================================================================== 3 passed in 0.04s ===========================================================================================
这里发现我们通过使用anjing_login 就直接调用了更改名前的login。
注意:当我们进行对fixture重命名后,如果再次调用以前的名字就会出现报错
import pytest
@pytest.fixture(name='mitu_login')
def login():
print('输入账号,输入密码')
print('完成登录功能!!!!')
yield
print('---退出登录---')
class Test_Login:
def test_01(self, login):
print('------用例01------')
def test_02(self):
print('------用例02------')
def test_03(self, mitu_login):
print('------用例03------')
if __name__ == '__main__':
pytest.main(['-s', 'test_demo.py'])
运行结果
========================================================================================= test session starts ==========================================================================================
platform win32 -- Python 3.9.12, pytest-7.3.1, pluggy-0.13.1
rootdir: D:\PycharmProjects\Source_Code\pytest_demo
plugins: allure-pytest-2.13.2, html-4.1.1, metadata-3.0.0
collected 3 items
test_demo.py E
test_demo.py::Test_Login::test_02.
SETUP F mitu_login
test_demo.py::Test_Login::test_03 (fixtures used: mitu_login).
TEARDOWN F mitu_login
================================================================================================ ERRORS ================================================================================================
_________________________________________________________________________________ ERROR at setup of Test_Login.test_01 _________________________________________________________________________________
file D:\PycharmProjects\Source_Code\pytest_demo\test_demo.py, line 14
def test_01(self, login):
E fixture 'login' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, extra, extras, include_metadata_in_junit_xml, metadata, mitu_login, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
D:\PycharmProjects\Source_Code\pytest_demo\test_demo.py:14
======================================================================================= short test summary info ========================================================================================
ERROR test_demo.py::Test_Login::test_01
====================================================================================== 2 passed, 1 error in 0.03s ======================================================================================
PS D:\PycharmProjects\Source_Code\pytest_demo>
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走,希望可以帮助到大家!领取资料,咨询答疑,请➕wei: June__Go