Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何测试列表中的所有元素_Python_Python 3.x_Pytest_Python 3.7 - Fatal编程技术网

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
,这样文件中的测试用例运行数就和整数一样多