如何创建一个新鲜的环境

问题描述 投票:0回答:1

这是我的工作流程:

我有一个requirements.txt 文件,其中包含tox 和许多其他包。

运行:

python3 -m venv global_env

source global_env/bin/activate

pip install -r requirements

我创建了一个 tox.ini 如下,并在 jenkins 文件中调用所有不同的环境,这真的非常好:

[tox]
envlist = py3.10.12, linter, unit-tests, docs 
skipsdist = true

[testenv]
allowlist_externals = pytest, sphinx-build
deps = 
    -rrequirements.txt
[testenv:linter]
description = Run Black linter
deps  = 
    black
commands = 
    black mtqe/ --check 

[testenv:unit-tests]
description = Run unit tests
deps = 
    {[testenv]deps}
commands =
    pytest ./tests/ --disable-warnings

我的理解是,tox 将创建一个完全全新的环境并运行测试。然而,事实并非如此。所有环境都继承自 global_env,我测试了从requirements.txt 中删除一些包,并且我预计某些单元测试应该失败,但它不会失败。我的理解有什么问题。那么tox如何创建一个隔离的环境呢?

python unit-testing pytest tox
1个回答
0
投票

我的理解是,tox 将创建一个完全全新的环境并运行测试。然而,事实并非如此。所有环境都继承自global_env

我确信

tox
创造新鲜的环境。我认为你问题的根源是
allowlist_externals = pytest
。您运行安装到 global_env 中的
pytest
,它会从其环境中导入所有内容,而不是从
tox
创建的环境中导入。要将
pytest
安装到
tox
创建的环境中:

[tox]
envlist = py3.10.12, linter, unit-tests, docs 
skipsdist = true

[testenv]
allowlist_externals = sphinx-build
deps = 
    -rrequirements.txt

[testenv:linter]
description = Run Black linter
deps  = 
    black
commands = 
    black mtqe/ --check 

[testenv:unit-tests]
description = Run unit tests
deps = 
    {[testenv]deps}
    pytest
commands =
    pytest ./tests/ --disable-warnings
© www.soinside.com 2019 - 2024. All rights reserved.