前言
python是目前广泛应用的编程语言,pytest 是对python程序进行test的工具。 pytest的使用有很多技巧,这篇文章中我想记录我在使用pytest中的一些经验和技巧而不涵盖pytest的基本使用方法,要想学习使用pytest的基本方法,读者需要阅读一些入门文档, 比如 https://docs.pytest.org/en/7.2.x/
1. 如何使用带参数的fixture函数
在 conftest.py 中:
@pytest.fixture(name="my_preparation")
def fixture_preparation(dataname):
"""pre-fixture
"""
with open(dataname, "rb") as fp:
repo_list = pickle.load(fp)
fp.close()
return repo_list
在测试文件中:
@pytest.unitest
@pytest.mark.parametrize("dataname", ["./temp/reposlist.pkl"])
@patch("server.server_response")
def test_get_repolist(mock_server_serverresponse, my_preparation):
mock_server_serverresponse.return_value = my_prepartion
....
....
....