Как проверить все элементы из списка - PullRequest
0 голосов
/ 30 января 2020

У меня есть список целых чисел. Я хочу указать каждое целое число в методе теста.

Вот как выглядит мой метод теста:

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 ?

1 Ответ

1 голос
/ 30 января 2020

Один из вариантов - использовать 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.

...