Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}
]
like image 240
Seanny123 Avatar asked Dec 30 '25 00:12

Seanny123


2 Answers

The correct format is:

env = [
    "DATA_DIR = data/test_assets",
    "ROSETTA = rosetta/main"
]
like image 110
Seanny123 Avatar answered Jan 03 '26 14:01

Seanny123


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.

like image 31
gold_cy Avatar answered Jan 03 '26 14:01

gold_cy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!