4

I'm using pytest to run some tests. Is there a way to specify multiple configurations through the pytest.ini?

The reason I need this is that when testing a locally running Lambda, I require some slightly different options than when running in CI pipeline.

For example, to check if tests are being run against a locally running Lambda, I check the value of an environment variable MYFUNCTION_LOCAL in the setup_class method of a test class. Additionally there are options such as --color no that don't necessarily need to be included when testing against a locally running Lambda.

Currently my 'pytest.ini' file looks like this. I can't add MYFUNCTION_LOCAL because the value will differ depending on where the tests are executed.

[pytest]
addopts = --color no --capture no --verbose
minversion = 7.0
env =
    AWS_XRAY_SDK_ENABLED=false

There seems to be the option of using a TOML file, but the documentation suggests that only the tool.pytest.ini_options table is considered at this time, so I don't think that is an option.

What is the best way to handle scenarios like this with pytest?

David Gard
  • 11,225
  • 36
  • 115
  • 227
  • Do you need to have it in pytest.ini? You can pass it as an argument and accept it in conftest.py to further process it. – Devang Sanghani Mar 18 '22 at 07:39
  • @DevangSanghani, thanks for the comment. I have considered using `pytest_addoption` to set whether or not the tests are being executed locally, but I'm finding that there are several other settings that are environment specific, so I wish to avoid really long CLI commands that have to typed every time I wish to run some tests. For that reason, it would be preferable to somehow configure multiple environments through **pytest.ini**. – David Gard Mar 18 '22 at 09:55
  • can you elaborate on what you're defining as configuration in this question. I think you can always add options at the command line too to merge with the options found in the ini file, I believe command line arguments take precedence. – jxramos Oct 11 '22 at 07:34
  • Does this answer your question? [How to pass environment variables to pytest](https://stackoverflow.com/questions/36141024/how-to-pass-environment-variables-to-pytest) – Yirmi Nov 04 '22 at 15:17

1 Answers1

2

You can create two separate ini files, then pass them with the -c arg

Quoting pytest --help

-c file

Load configuration from file instead of trying to locate one of the implicit configuration files

For example:

  • pytest -c lambda-pytest.ini
  • pytest -c ci-pytest.ini
Yirmi
  • 332
  • 2
  • 13