I setting up tox to be used in my .gitlab-ci.yml in combination with pytest.
I want pytest to run with specific environment that are set in the .gitlab-ci.yml file
In the documentation I found the tox.ini configurations setenv and passenv but I couldn't understand what is the use case for each configuration.
What is the difference? What are the use cases? i.e. when would I want to use setenv over passenv or vice versa?
setenvexpects you to set a value explicitly:passenvdoes not require a value to be set -- it should be passed from the context in whichtoxis invoked. As relevant background... withoutpassenv, any environment variables you may have set before invokingtoxwon't be present in the testing environment created bytox!For example, I might have some tests that I know can run locally, but will not work when run in a CI environment. So, in pytest, I might have something like this:
In this case I don't want to use
setenvbecause I still want these tests to run when I invoketoxlocally, but not in CI. So, I configurepassenvfor this variable to ensure the variableCI(which is always present in the CI system) is accessible in the tox environment:In other cases, I may want a variable to always be set to a specific value whenever
toxis invoked, for example, thePYTHONUNBUFFEREDvariable to ensure output from my tests is not buffered:Another unique usecase for
passenvis that it supports using*and?for glob-like matching of multiple environment variables. For example, perhaps your tests make use of many GitHub Actions environment variables (which are namespaced with with the prefixGITHUB_), you might do something like this:Or for GitLab CI's environment variables which are prefixed with
CI_andGITLAB_:Lastly,
setenvvalues take precedence overpassenvvalues. So, even if a wildcard inpassenvmatches a present environment variable, usingsetenvwill take precedence overpassenv, ensuring the variable/value you specify takes effect over any value that may have been specified or matched withpassenv.