我有一个整数列表。我想为测试方法提供每个整数。
这是我的测试方法的外观:
my_test.py
:
import pytest
def test_mt(some_number):
assert(some_number == 4)
conftest.py
:
@pytest.fixture(scope="session", autouse=True)
def do_something(request):
#Will be read from a file in real tests.
#Path to the file is supplied via command line when running pytest
list_int = [1, 2, 3, 4]
#How to run test_mt test case for each element of the list_int
如果我准备了一些要测试的参数,如何将它们提供给由test_mt
我的功能?
一个选项是使用parametrize
代替fixture
def do_something():
#Will be read from a file in real tests.
#Path to the file is supplied via command line when running pytest
list_int = [1, 2, 3, 4]
for i in list_int:
yield i
@pytest.mark.parametrize('some_number', do_something())
def test_mt(some_number):
assert(some_number == 4)
这将运行4次测试,每次some_number
中使用不同的数字。