1

I have a list of integers. I want to supply each integer to the test method.

Here is how my test method looks like:

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

In case I prepared some arguments to be tested how can I supply them to a test case defined by the test_mt function in my case?

Some Name
  • 8,555
  • 5
  • 27
  • 77

1 Answers1

4

One option is to use parametrize instead of 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)

This will run the test 4 times, each time with different number in some_number.

Guy
  • 46,488
  • 10
  • 44
  • 88