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

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

Open
bram-tv opened this issue Feb 27, 2024 · 0 comments

Comments

@bram-tv
Copy link

bram-tv commented Feb 27, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant