Seanny123
Seanny123

Reputation: 9346

How to insert test-time environment variable with pytest pyproject.toml

Usually, when defining a Environment Variable for pytest using the pytest-env plugin, this is done using pytest.ini:

[pytest]
env =
    DATA_DIR = data/test_assets

pytest supports pyproject.toml since 6.0. How is an Environment Variable supposed be defined in this format? The following does not work:

[tool.pytest.ini_options]
env = [
    {DATA_DIR = "data/test_assets"}
]

Upvotes: 14

Views: 10075

Answers (2)

Seanny123
Seanny123

Reputation: 9346

The correct format is:

env = [
    "DATA_DIR = data/test_assets",
    "ROSETTA = rosetta/main"
]

Upvotes: 22

gold_cy
gold_cy

Reputation: 14236

So the issue resides with the pytest-env plugin. First of all, it seems to have been abandoned by its maintainers as it has not been updated since 2017. It comes down to how the plugin parses the part of the toml file as shown here.

The issue is specifically this line. When parsing the contents of your pyproject.toml it transforms what you have in to the following:

part = e.partition("=")
# ('{DATA_DIR ', '=', ' "data/test_assets"}')
key = part[0].strip()
# '{DATA_DIR'

That means it transforms your environment variable to have the key of '{DATA_DIR' hence why it does not seem to be working as expected. You need to either switch back to the previous format you were using for environment variables or remove the curly brackets as they seem to not mesh with this plugin.

Upvotes: 5

Related Questions