Python 如何测试列表中的所有元素
我有一个整数列表。我想为测试方法提供每个整数 以下是我的测试方法:Python 如何测试列表中的所有元素,python,python-3.x,pytest,python-3.7,Python,Python 3.x,Pytest,Python 3.7,我有一个整数列表。我想为测试方法提供每个整数 以下是我的测试方法: my_test.py: import pytest def test_mt(some_number): assert(some_number == 4) @pytest.fixture(scope="session", autouse=True) def do_something(request): #Will be read from a file in real tests. #Path to t
my_test.py
:
import pytest
def test_mt(some_number):
assert(some_number == 4)
@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
conftest.py
:
import pytest
def test_mt(some_number):
assert(some_number == 4)
@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\u mt
函数定义的测试用例?一个选项是使用参数化
而不是夹具
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次,每次在
一些编号中使用不同的编号
一个选项是使用参数化
而不是夹具
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次,每次在
某些\u编号中使用不同的编号
是否要将test\u mt()函数应用于列表的每个元素?使用for loop?@ChayanBansal。在pytest
中有没有办法做到这一点?正如@HarshaBiyani所说,你可以使用for循环,甚至使用mapfunction@HarshaBiyani我想运行python-m pytest--testcases/path/to/the/file
,这样文件中的测试用例运行数就和整数一样多了您想应用测试吗函数,精确地使用for loop@ChayanBansal对列表中的每个元素执行。在pytest
中有没有办法做到这一点?正如@HarshaBiyani所说,你可以使用for循环,甚至使用mapfunction@HarshaBiyani我想运行python-mpytest--testcases/path/to/the/file
,这样文件中的测试用例运行数就和整数一样多