Skip to content

Override native toml ([tool.pytest_env]) with (pytest.ini) configuration in sub dir #75

@bram-tv

Description

@bram-tv

A summary of our use case:

  • we have tests in a tests/ directory which requires a certain set of ENV vars
  • we have tests in a tests_integration directory which requires a different set of ENV vars

We are also using poetry in combination with the pyproject.toml file and when using poetry run pytest it runs the tests in tests/ directory and when using poetry run pytest tests_integration/ it runs the tests in the tests_integration directory.

Let's start with a concrete example of what we want and what currently works.

pyproject.toml

[tool.pytest.ini_options]
addopts = ["-v"]
pythonpath = ["src"]
testpaths = ["tests"]
env = [
    "TEST_ENV=unit",
]

tests/tests_env.py:

from os import environ

def test_env():
    assert environ.get("TEST_ENV", "") == "unit"

tests_integration/test_env.py:

from os import environ

def test_env():
    assert environ.get("TEST_ENV", "") == "integration"

tests_integration/pytest.ini:

[pytest]
addopts = -v
env =
    TEST_ENV=integration

Running it:

$ poetry run pytest
...
tests/test_env.py::test_env PASSED                                                                             [100%]
...
$ poetry run pytest tests
...
tests/test_env.py::test_env PASSED                                                                             [100%]
...
$ poetry run pytest tests_integration/
...
tests_integration/test_env.py::test_env PASSED                                                                 [100%]

The above is working how we want it to work.

Moving to the native toml syntax for pytest-env:
pyproject.toml changed to:

[tool.pytest.ini_options]
addopts = ["-v"]
pythonpath = ["src"]
testpaths = ["tests"]

[tool.pytest_env]
TEST_ENV = "unit"

rest of the files kept as-is

Running again:

$ poetry run pytest
...
tests/test_env.py::test_env PASSED                                                                             [100%]
$ poetry run pytest tests
...
tests/test_env.py::test_env PASSED                                                                             [100%]
$ poetry run pytest tests_integration/
...
tests_integration/test_env.py::test_env FAILED                                                                 [100%]

======================================================= FAILURES =======================================================
_______________________________________________________ test_env _______________________________________________________

    def test_env():
>       assert environ.get("TEST_ENV", "") == "integration"
E       AssertionError: assert 'unit' == 'integration'
E         - integration
E         + unit

tests_integration/test_env.py:4: AssertionError

What we're looking for is to be able to:

  • use the native toml syntax (since that is cleaner)
  • override the environment variables in a sub-dir (doesn't have to be via pytest.ini, other mechansims could also work)

(An accepted/expected limitation in the above is that pytest will never run tests/ and tests_integration/ at the same time/in the same process)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions