tox网站上列出的第一个功能是"使用不同的Python版本和解释器正确检查您的软件包安装".这让我觉得,如果我搞砸了我的setup.py,测试将无法通过,我会被警告我的setup.py已经坏了.
事实并非如此.事实上,如果您遵循将模块或包放在与tox.ini相同的目录中的通常模式,则代码将从本地目录(aka {toxinidir})导入.这意味着你可以让你的setup.py绝对没有任何东西,tox会告诉你它的罚款.那时,在你推到pypi并尝试使用之前,很容易就不会注意到这个问题.这是我想要防止的问题.
主要问题是在测试期间空字符串出现在sys.path上.Python将此解释为当前工作目录并从那里导入.
有没有办法配置tox,以便在测试期间不使用本地目录?
目前我的解决方法是cd {envtmpdir} && coverage run && mv .coverage {toxinidir}
,但这显然是一个丑陋的黑客.
在这里,我提供了两个版本的代码:
我的目标是找到一个避免上述问题的tox配置,并且足够合理,建议在所有项目中使用.
我目前有一个包含以下.travis.yml
文件的项目:
language: python
install: "pip install tox"
script: "tox"
Run Code Online (Sandbox Code Playgroud)
在本地,tox
正确执行并运行35个测试,但在Travis CI上,它运行0个测试.
更多细节:https://travis-ci.org/neverendingqs/pyiterable/builds/78954867
我也尝试过其他方式,包括:
language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
- "3.4"
- "3.5.0b3"
- "3.5-dev"
- "nightly"
# also fails with just `nosetest` and no `install` step
install: "pip install coverage unittest2"
script: "nosetests --with-coverage --cover-package=pyiterable"
Run Code Online (Sandbox Code Playgroud)
他们也找不到任何测试.
我的项目结构是这样的:
- ...
- <module>
- tests (for the module)
- ...
Run Code Online (Sandbox Code Playgroud)
项目/文件夹结构不正确吗?
我试图在将命令放入 tox.ini[testenv] commands =
部分之前验证命令。
是否可以通过将自定义命令tox
作为 shell 参数传递来传递给它?就像是
tox -e <env_to_run_script_in> <command_which_we_want_to_run_in_specified_env>
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法,但没有一个有效。
tox -e py34 args py.test
tox -e py34 -- py.test
tox args "py.test"
Run Code Online (Sandbox Code Playgroud)
如何在 tox 创建的虚拟环境中运行 python 命令/脚本而不将它们放在 tox.ini 中?
我正在使用tox
并coverage.py
在我的连续构建服务器中运行我的Python项目的测试.我也有一个pkg_x
来自我已安装使用的供应商的包(在PyPI上不可用),python3.5 setup.py install
它将其放入/usr/lib/python3.5/site-packages
.现在我需要将该包用于测试代码.
我的当前tox.ini
看起来像这样:
[tox]
envlist = py35
[testenv]
deps = nose
coverage
commands = coverage run -m nose []
sitepackages = True
Run Code Online (Sandbox Code Playgroud)
我像这样运行测试:
python3.5 -m tox -- --verbose --with-doctest
Run Code Online (Sandbox Code Playgroud)
这失败了 - 没有找到我本地列出的依赖包setup.py
(例如像公共的东西more_itertools
),即使它确实创建了类似于.tox/py35/lib/python3.5/site-packages/more_itertools
包含相关包的目录.如果我开火.tox/py35/bin/python3.5
,sys.path
看起来像这样:
>>> [re.compile('.*\\.tox').sub('.tox', x) for x in sys.path]
['',
'.tox/py35/lib64/python35.zip',
'.tox/py35/lib64/python3.5',
'.tox/py35/lib64/python3.5/plat-linux',
'.tox/py35/lib64/python3.5/lib-dynload',
'/usr/lib64/python3.5',
'/usr/lib/python3.5',
'.tox/py35/lib/python3.5/site-packages']
Run Code Online (Sandbox Code Playgroud)
如果我删除了我的sitepackages = True
行tox.ini
,那么我确实得到了更多,在那些包中,现在可以找到more_itertools
我的setup.py …
目标:成功执行特定的 tox 命令,并让它为“仅”匹配的特定命令运行。
例子: tox -e py35-integration
tox
应该只为 py35-integration 运行,不包括默认或独立py35
定义。
我尝试了两种不同的方法,据我所知,这是尝试做我想做的事情的两种方法。
flake8
命令是为了轻松隔离不同的命令并向我指示正在运行的内容。这并不是我真正想要运行的命令的指示。此外,ini 文件仅显示相关部分。
[tox]
envlist = {py27,py35}, {py27,py35}-integration
[testenv]
commands =
py27: python -m testtools.run discover
py35: python -m testtools.run discover
py27-integration: flake8 {posargs}
py35-integration: flake8 {posargs}
Run Code Online (Sandbox Code Playgroud)
使用这种方法,这里的理解是我希望在tox -e py27-integration
不运行为py27
命令定义的内容的情况下运行。这不是正在发生的事情。相反,它将同时运行py27
和py27-integration
。
[tox]
envlist = {py27,py35}, {py27,py35}-integration
[testenv]
commands =
python -m testtools.run discover
[testenv:integration]
commands =
flake8 {posargs}
Run Code Online (Sandbox Code Playgroud)
现在,我在这里明确地隔离了一个“子”环境,它有自己的命令来运行“集成”。
但是,不幸的是,我遇到了与正在执行的所有匹配模式“py27”完全相同的行为。 …
我正在添加单元测试和一种“传统”Python 包。一些模块包含嵌入在 docstrings 中的自己的 doctests。我的目标是运行这些文档测试和新的专用单元测试。
下面这个Q&A(“如何让py.test运行文档测试以及正常的测试目录?”)我使用的--doctest-modules
选项pytest
。当从源存储库运行时,pytest
确实从src
目录下的 Python 模块中发现了嵌入的 doctests 。
但是,我的目标是测试源发行版是否完全构建和安装,然后针对已安装的包测试所有内容。为此,我正在使用tox
which 自动构建sdist
(源代码分发)tarball 的过程,将其安装在虚拟环境中,并针对已安装的版本运行测试。为了确保它是已安装的版本,而不是源存储库中由测试导入的版本,我遵循本文中的建议,存储库现在如下所示:
repo/
src/
my_package/
__init__.py
module_a.py
module_b.py
...
tests/
test_this.py
test_that.py
requirements.txt
setup.py
tox.ini
Run Code Online (Sandbox Code Playgroud)
(下面的测试脚本tests
将包导入为 in import my_package
,它会命中已安装的版本,因为存储库布局确保src/my_package
目录不在模块搜索路径中。)
在tox
配置文件中,相关部分看起来像
[tox]
envlist = py27,py36,coverage-report
[testenv]
deps =
-rrequirements.txt
commands =
coverage run -p -m pytest --
[pytest]
addopts = --doctest-modules
Run Code Online (Sandbox Code Playgroud)
到目前为止,测试运行良好,并且 doctests …
我在我的项目中添加了 tox,我的tox.ini
很简单:
[tox]
envlist = py37
[testenv]
deps =
-r{toxinidir}/requirements_test.txt
commands =
pytest -v
Run Code Online (Sandbox Code Playgroud)
但是当我运行时tox
,出现以下错误:
[tox]
envlist = py37
[testenv]
deps =
-r{toxinidir}/requirements_test.txt
commands =
pytest -v
Run Code Online (Sandbox Code Playgroud)
这是我的setup.py
:
ERROR: invocation failed (exit code 1), logfile: /path/to/my_project/.tox/py37/log/py37-2.log
========================================================================================= log start ==========================================================================================
Processing ./.tox/.tmp/package/1/my_project-0+untagged.30.g6909bfa.dirty.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-req-build-ywna_4ks/setup.py", line 15, in <module>
with open(requirements_path) as requirements_file:
FileNotFoundError: [Errno 2] No …
Run Code Online (Sandbox Code Playgroud) 我是 tox 和 GitHub 操作的新手,我正在寻找一种使它们协同工作的简单方法。我写了这个简单的工作流程:
name: Python package
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.7]
steps:
- uses: actions/checkout@v1
- name: Install tox
run: pip install tox
- name: Run tox
run: tox
Run Code Online (Sandbox Code Playgroud)
它只是安装 tox 然后运行它。但是当我运行此工作流时,tox 安装工作正常,但运行命令返回错误:
tox: command not found
Run Code Online (Sandbox Code Playgroud)
从 GitHub 操作运行 tox 的正确方法是什么?
提前致谢,如果有任何帮助。
我打算在 pike 分支中使用 openstack/horizon,如下所示:
git clone https://github.com/openstack/horizon.git
cd horizon
git checkout -b pike remotes/origin/stable/pike
tox -e runserver
Run Code Online (Sandbox Code Playgroud)
它失败并显示以下消息:
runserver create: /Users/xiaojueguan/code/OpenStack/horizon/.tox/runserver
runserver installdeps: -r/Users/xiaojueguan/code/OpenStack/horizon/requirements.txt, -r/Users/xiaojueguan/code/OpenStack/horizon/test-requirements.txt
ERROR: invocation failed (exit code -6), logfile: /Users/xiaojueguan/code/OpenStack/horizon/.tox/runserver/log/runserver-2.log
=========================================================== log start ============================================================
============================================================ log end =============================================================
____________________________________________________________ summary _____________________________________________________________
ERROR: runserver: InvocationError for command /Users
Run Code Online (Sandbox Code Playgroud)
所以我将 -vvv 添加到 tom 命令中,如下所示:
tox -e runserver -vvv
Run Code Online (Sandbox Code Playgroud)
它显示以下消息:
using tox.ini: /Users/xiaojueguan/code/OpenStack/horizon/tox.ini (pid 53225)
removing …
Run Code Online (Sandbox Code Playgroud) 这是我的 tox.ini 文件:
dino@DINO:~/code/mplfinance$ cat tox.ini
[tox]
envlist = py36, py37, py38
[pytest]
python_files = tests.py
[testenv]
deps =
matplotlib
numpy
pandas
pytest
setenv =
# don't use interactive backend for matplotlib in e2e tests
MPLBACKEND = agg
commands =
pytest
Run Code Online (Sandbox Code Playgroud)
当我运行tox<Enter>
它时,它会为指定的三个环境中的每一个启动 pytest。当 pytest 首次启动时(对于每个环境),它会报告 Python 的版本以及它正在使用的其他一些东西。例如,py36
我看到:
py36 runtests: PYTHONHASHSEED='893013612'
py36 runtests: commands[0] | pytest
============= test session starts ==============
platform linux -- Python 3.6.7, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /home/dino/code/mplfinance, inifile: tox.ini
collected 2 items
tests.py …
Run Code Online (Sandbox Code Playgroud)