Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define a dynamic filename for parametrized tests for py.test? #140

Open
alex4200 opened this issue Jul 7, 2022 · 2 comments
Open
Labels
pytest-fixtures Issues related to pytest fixture support.

Comments

@alex4200
Copy link

alex4200 commented Jul 7, 2022

In a py.test code I can specify some file that can be used for some tests. So in the conftest.py I define

def pytest_addoption(parser):
    """Defines the extra options for the MOOC tests."""
    parser.addoption(
        "--tests",
        help="Defines the json files containing the test parameters.",
    )
  

@pytest.fixture
def testfile(request):
    """Returns True if driver should run headless."""
    return request.config.getoption("--tests")

so I have a fixture which represents a filename.

Now in the test itself I want to use this filename to parametrize a test as follows:

from parameterized import parameterized

    @parameterized(
        param.explicit(*json.loads(line))
        for line in open(testfile)
    )
    def test_1(testparam):
         ...

but that does not work as the fixture testfile does not seem to be defined/accessible in "front of" a test method. Is there any way to define testfile in a dynamic way?

@alexlausz
Copy link

In a py.test code I can specify some file that can be used for some tests. So in the conftest.py I define

def pytest_addoption(parser):
    """Defines the extra options for the MOOC tests."""
    parser.addoption(
        "--tests",
        help="Defines the json files containing the test parameters.",
    )
  

@pytest.fixture
def testfile(request):
    """Returns True if driver should run headless."""
    return request.config.getoption("--tests")

so I have a fixture which represents a filename.

Now in the test itself I want to use this filename to parametrize a test as follows:

from parameterized import parameterized

    @parameterized(
        param.explicit(*json.loads(line))
        for line in open(testfile)
    )
    def test_1(testparam):
         ...

but that does not work as the fixture testfile does not seem to be defined/accessible in "front of" a test method. Is there any way to define testfile in a dynamic way?

Facing the same problems

@wolever
Copy link
Owner

wolever commented Mar 2, 2023

Hey! That's a great question, and I'm not sure I know of a good solution right now.

Getting pytest fixtures playing nicely with parameterized is an open issue, and I suspect that would solve this problem.

And in the meantime, a functional but potentially less good solution could be to pass in parameters with an environment variable:

$ cat test.py
…
@parameterized(json.loads(os.environ["DATA_FILE"]))
def test_data_file(…):
   …
…
$ DATA_FILE=foo.json pytest

@wolever wolever added the pytest-fixtures Issues related to pytest fixture support. label Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pytest-fixtures Issues related to pytest fixture support.
Projects
None yet
Development

No branches or pull requests

3 participants