I want to run my test with different values of environment variables. I have this tox.ini, which doesn't do what I want:
# tox.ini
[tox]
envlist = py37-{foo,bar}
[testenv]
description = Tests common
setenv =
MY_VAR=COMMON
commands =
env
[testenv:foo]
description = Tests foo
setenv =
MY_VAR=FOO
[testenv:bar]
description = Tests bar
setenv =
MY_VAR=BAR
Above ini produced the following output:
$ tox
GLOB sdist-make:
***
py37-foo run-test: commands[0] | env
***
MY_VAR=COMMON <<<--- MY_VAR=foo is expected
***
py37-bar run-test: commands[0] | env
***
MY_VAR=COMMON <<<--- MY_VAR=bar is expected
Whats wrong?
I use:
Win 10.0.18363 Build 18363
Python 3.7.4
tox: 3.14.0
tox doesn't combine environments. Every environment is derived from [testenv] but other environments are not considered for inclusion. You have to combine them yourself in tox.ini. This should work:
[tox]
envlist = py37-{foo,bar}
[testenv]
description = Tests common
setenv =
MY_VAR=COMMON
commands =
env
[foo]
description = Tests foo
setenv =
MY_VAR=FOO
[bar]
description = Tests bar
setenv =
MY_VAR=BAR
[testenv:py37-foo]
description = Tests py37 foo
setenv = {[foo]setenv}
[testenv:py37-bar]
description = Tests py37 bar
setenv = {[bar]setenv}
The key is the Compressing dependency matrix. This technique results compact, and non-redundant solution:
[tox]
envlist = py37-{foo,bar,baz}
[testenv]
setenv =
MY_VAR=COMMON
foo: MY_VAR=FOO
bar: MY_VAR=BAR
commands =
env
Related
Below is the failure message on running tox. I don't see this exact error reported on any forum.
Any guidance here would be of great help.
I'm invoking the tox in 3.8-slim-buster docker container & installed the required dependencies - pip install tox flake8 black pylint
Error:
File ".tox/lint/lib/python3.6/site-packages/hacking/core.py", line 185
except ImportError, exc:
^
SyntaxError: invalid syntax
During handling of the above exception, another exception occurred:
.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 168, in load_plugin
raise failed_to_load
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "H000" due to invalid syntax (core.py, line 185).
my tox.ini file.
[tox]
minversion = 1.8
envlist =
unit
lint
format-check
skipsdist = true
[testenv]
usedevelop = true
basepython = python3
passenv = *
setenv =
COVERAGE_FILE={toxworkdir}/.coverage
PIP_EXTRA_INDEX_URL=https://maven.com/artifactory/api/pypi/simple/
extras =
test
[testenv:unit]
commands =
python -m pytest {posargs}
[testenv:lint]
commands =
python -m flake8
[testenv:format]
commands =
python -m black {toxinidir}
[testenv:format-check]
commands =
python -m black --diff --check {toxinidir}
[testenv:build-dists-local]
usedevelop = false
skip_install = true
commands =
python -m pep517.build \
--source \
--binary \
--out-dir {toxinidir}/dist/ \
{toxinidir}
[testenv:build-dists]
commands =
rm -rfv {toxinidir}/dist/
{[testenv:build-dists-local]commands}
whitelist_externals =
rm
[testenv:publish-dists]
commands =
bash -c '\
twine upload {toxinidir}/dist/*.whl \
-u $TWINE_USERNAME \
-p $TWINE_PASSWORD \
--repository-url $TWINE_REPOSITORY \
'
whitelist_externals =
bash
[flake8]
max-line-length = 100
format = pylint
exclude =
.eggs/
.tox/,
.venv*,
build/,
dist/,
doc/,
#- [H106] Don't put vim configuration in source files.
#- [H203] Use assertIs(Not)None to check for None.
#- [H904] Delay string interpolations at logging calls.
enable-extensions = H106,H203,H904
ignore = E226,E302,E41
[pytest]
testpaths = test/
addopts = -v -rxXs --doctest-modules --cov metarelease --cov-report term-missing --showlocals
norecursedirs = dist doc build .tox .eggs
[coverage:run]
omit =
metarelease/cmd/*
metarelease/shell.py
[coverage:report]
fail_under =
100
This is neither a flake8 nor a tox bug, but a bug in hacking, which you will notice when you have a close look at the traceback.
The syntax
File ".tox/lint/lib/python3.6/site-packages/hacking/core.py", line 185
except ImportError, exc:
is only valid in Python 2, but you use Python 3.
I never heard of the hacking project before, but using a search engine revealed https://pypi.org/project/hacking/
You should report a bug a their bug tracker.
In my Python project, I'm reading environment variables from a .env file. I am actually using pydantic to read/verify the env vars.
When using tox, the .env file will be completely ignored. I am wondering how to make tox acknowledging the existence of .env?
Here's my tox.ini
[tox]
envlist = py39
[testenv]
deps = -r requirements-dev.txt
commands = pytest {posargs}
My .env file:
ENV_STATE="prod" # dev or prod
At first, I thought maybe pydantic loads the content of the .env file as environment variables, that is why I wrote this as my first answer:
original answer
tox does some isolation work, so your builds / tests are more reproducible.
This means that e.g. environment variables are filtered out, except you whitelist them.
You probably need to set
passenv = YOUR_ENVIRONMENT_VARIABLE
Also see in the tox documentation.
updated answer
This does not seem to be a tox issue at all.
I just created a simple project with pydantic and dotenv, and it works like a charm with tox.
tox.ini
[tox]
envlist = py39
skipsdist = True
[testenv]
deps = pydantic[dotenv]
commands = pytest {posargs}
.env
ENVIRONMENT="production"
main.py
from pydantic import BaseSettings
class Settings(BaseSettings):
environment: str
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
test_main.py
from main import Settings
def test_settings():
settings = Settings(_env_file=".env")
assert settings.environment == "production"
This is the path to the project
D:\QA\test-framework\python-client
This is a test frame work implemented by python
This is the python file that contains tests
This is the path to the test case that I need to run
D:\QA\test-framework\python-client\test_data\tests\curve.json
This is the beginning of the curve.json file.
{
"Sklearn - Sklearn - Regression - Curve M2" : [
{
"dataImport": {
"
"
"
]
}
This is the tox.ini file
[tox]
envlist = py38
[testenv]
deps =
pytest
pytest-html
pytest-sugar
pytest-logger
allure-pytest
pytest-xdist
pytest_steps
datetime
oauth2client
gspread
aiclub
commands =
pytest -s -v -k _workflow --html=test_report.html --alluredir=allure-
results/ -n auto --dist=loadfile
allure serve allure-results
pytest {posargs}
I need to run only this curve.json using tox command
I want to specifically run a certain tox section which then auto-decides on the specific platform.
The example code-snippet below works fine if I just ran tox -e ALL. Then the platform condition nicely sects out the correct platform.
However, I want to only adress and run a specific section like for instance something like tox -e other (not tox -e other-win, other-linux) and then have tox auto-chosing the corresponding platform (or any other) condition.
I don't know if this way of setting up conditions in tox is not possible, or if I'm missing something.
[tox]
skipsdist = true
[testenv:systest-{win, linux}]
platform =
linux: linux
win: win|msys
whitelist_externals =
win: cmd
linux: sh
commands =
win: cmd /r echo {env:OS}
linux: sh -c echo {env:OS}
[testenv:other-{win, linux}]
platform =
linux: linux
win: win|msys
whitelist_externals =
win: cmd
linux: sh
commands =
win: cmd /r echo {env:OS}
linux: sh -c echo {env:OS}
You could give the tox-factor plugin a try.
For example:
tox.ini
[tox]
envlist =
alpha-{redmond,tux}
bravo-{redmond,tux}
requires =
tox-factor
skipsdist = true
[testenv]
commands =
python -c 'import sys; print("platform", sys.platform)'
platform =
redmond: win32
tux: linux
This gives the following four environments:
$ tox --listenvs
alpha-redmond
alpha-tux
bravo-redmond
bravo-tux
That can be selected according to the factors:
$ tox --listenvs --factor tux
alpha-tux
bravo-tux
$ tox --listenvs --factor alpha
alpha-redmond
alpha-tux
And then run like this (for example on a Linux platform):
$ tox --factor bravo
bravo-tux run-test-pre: PYTHONHASHSEED='1770792708'
bravo-tux run-test: commands[0] | python -c 'import sys; print("platform", sys.platform)'
platform linux
________________________________________________ summary ________________________________________________
SKIPPED: bravo-redmond: platform mismatch ('linux' does not match 'win32')
bravo-tux: commands succeeded
congratulations :)
References:
https://github.com/tox-dev/tox/issues/1338
https://pypi.org/project/tox-factor/
e.g. Let's say I have following in tox.ini
[tox]
envlist = py27, py35, testenv2
[testenv]
# settings related to "default" testenv - includes deps, commands
[testenv:testenv2]
# settings related to testenv2 - includes deps, commands
Now when I run tox command, it invokes testenv commands with python 2.7 and 3.5 interpreter but testenv2 commands only with base python installed on the machine (in my case 2.7). How to get tox to also test a "named" (non-default) test environment like testenv2 to be tested with multiple python versions?
The first answer describes two viable ways. For completeness: you can also generate environments and use conditional settings - e.g.:
[tox]
skipsdist = True
envlist = py{27,35}-{test,lint}
[testenv]
skip_install = True
deps =
test: pytest
test: pytest-xprocess
lint: flake8
lint: black
commands =
test: pytest -v
lint: flake8
lint: black .
would generate (tox -a):
py27-test
py27-lint
py35-test
py35-lint
You can use any factor (e.g. py27 or test) to conditionally add commands, deps, etc.
See also the docs.
BTW: to see which settings each testenv exactly has you can run tox --showconfig.
List all environments explicitly:
[tox]
envlist = py27, py35, py27-testenv2, py35-testenv2
If that's not enough refactor tox.ini:
[testenv]
# settings related to "default" testenv - includes deps, commands
[testenv2]
# settings related to testenv2 - includes deps, commands
[testenv:py27-testenv2]
deps = {[testenv2]deps}
commands = {[testenv2]commands}
[testenv:py35-testenv2]
deps = {[testenv2]deps}
commands = {[testenv2]commands}
I could get the "named" test environment to be tested with multiple python versions by making multiple "named" test environments, one each for the different python versions I wanted it to be tested with and using the basepython option to specify the python version for the test environment to be tested with. Below is the example that works:
[tox]
envlist = py27, py35, testenv2_py27, testenv2_py35
[testenv]
# settings related to "default" testenv - includes deps, commands
[testenv:testenv2_py27]
basepython = python2.7
# settings related to testenv2 - includes deps, commands
[testenv:testenv2_py35]
basepython = python3.5
# settings related to testenv2 - includes deps, commands
Just in case someone still needs this, I figured out an even more concise solution. It combines all the good suggestions from the other answers:
[tox]
envlist = py{27,35}, testenv2-py{27,35}
[testenv]
# settings related to "default" testenv - includes deps, commands
[testenv:testenv2-py{27,35}]
# settings related to testenv2 - includes deps, commands