如何测试列表中的所有元素

问题描述 投票:0回答:1

我有一个整数列表。我想为测试方法提供每个整数。

这是我的测试方法的外观:

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我的功能?

定义的测试用例
python python-3.x pytest python-3.7
1个回答
1
投票

一个选项是使用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中使用不同的数字。

© www.soinside.com 2019 - 2024. All rights reserved.