python - 安装 pytest-xdist 后 Pytest 无法识别 -n 选项

标签 python pytest xdist pytest-xdist

我已经在工作的 pytest 环境之上安装了 pytest-xdist :

pip install pytest-xdist

我已经收到了这个输出

Downloading/unpacking pytest-xdist
  Downloading pytest-xdist-1.10.tar.gz
  Running setup.py egg_info for package pytest-xdist

    no previously-included directories found matching '.hg'
Downloading/unpacking execnet>=1.1 (from pytest-xdist)
  Downloading execnet-1.2.0.tar.gz (163kB): 163kB downloaded
  Running setup.py egg_info for package execnet

    warning: no files found matching 'conftest.py'
Requirement already satisfied (use --upgrade to upgrade): pytest>=2.4.2 in /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages (from pytest-xdist)
Requirement already satisfied (use --upgrade to upgrade): py>=1.4.20 in /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages (from pytest>=2.4.2->pytest-xdist)
Installing collected packages: pytest-xdist, execnet
  Running setup.py install for pytest-xdist

    no previously-included directories found matching '.hg'
  Running setup.py install for execnet

    warning: no files found matching 'conftest.py'
Successfully installed pytest-xdist execnet
Cleaning up...

此时我尝试并行运行我的测试套件

py.test -n 4

但我收到的是这个输出

usage: py.test [options] [file_or_dir] [file_or_dir] [...]
py.test: error: unrecognized arguments: -n

“py.test --version is”的输出

This is pytest version 2.6.2, imported from /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages/pytest.pyc
setuptools registered plugins:
  pytest-capturelog-0.7 at /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages/pytest_capturelog.pyc
  pytest-contextfixture-0.1.1 at /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages/pytest_contextfixture.pyc
  pytest-cov-1.7.0 at /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages/pytest_cov.pyc
  pytest-django-2.6.2 at /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages/pytest_django/plugin.pyc
  pytest-pydev-0.1 at /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages/pytest_pydev.pyc
  pytest-runfailed-0.3 at /Users/sal/Documents/code/Python/VirtualEnv/Spring/lib/python2.7/site-packages/pytest_runfailed.pyc

并且实际上缺少 pytest-xdist。

我哪里错了?谢谢。

最佳答案

user2412166 ,我遇到了同样的问题。与 user2412166 不同,我的解决方案是放宽对 pip3 安装的 xdistpytest_xdist-1.14.dist-info 系统目录的权限.

一些背景故事:为了安全起见,我在我的系统上运行了一个严格的umask,禁止所有访问其他用户和写访问group 默认用户:

$ umask
027

虽然这通常是件好事,但偶尔也会给我带来麻烦。通过umask下的pip3安装python-xdist:

$ sudo pip3 install pytest-xdist

...导致 pip3 禁止非 super 用户进行读取和执行访问——最好是只有我:

$ ls -l /usr/lib64/python3.4/site-packages/xdist
drwxr-x---   3 root root 4.0K 2016-04-10 01:19 xdist/
$ ls -l /usr/lib64/python3.4/site-packages/pytest_xdist-1.14.dist-info
drwxr-x---   3 root root 4.0K 2016-04-10 01:19 xdist/

虽然 pip3 这样做并没有错,但是 py.test 是(...有争议的!)错误的是默默地忽略而不是在插件检测期间明确报告明显的权限问题。

这可以通过递归地授予 other 用户对受影响的系统目录的读取和目录执行权限来轻松解决:

$ chmod -R o+rX /usr/lib64/python3.4/site-packages/xdist
$ chmod -R o+rX /usr/lib64/python3.4/site-packages/pytest_xdist-1.14.dist-info

证明是命令行布丁:

$ ls -l /usr/lib64/python3.4/site-packages/xdist
drwxr-xr-x   3 root root 4.0K 2016-04-10 01:19 xdist/
$ ls -l /usr/lib64/python3.4/site-packages/pytest_xdist-1.14.dist-info
drwxr-xr-x   3 root root 4.0K 2016-04-10 01:19 xdist/
$ py.test --version
This is pytest version 2.8.7, imported from /usr/lib64/python3.4/site-packages/pytest.py
setuptools registered plugins:
  pytest-xdist-1.14 at /usr/lib64/python3.4/site-packages/xdist/looponfail.py
  pytest-xdist-1.14 at /usr/lib64/python3.4/site-packages/xdist/plugin.py
  pytest-xdist-1.14 at /usr/lib64/python3.4/site-packages/xdist/boxed.py

这样,不清楚的地方就清楚了,bug 被调试了,缓慢的测试很快就并行了。

关于python - 安装 pytest-xdist 后 Pytest 无法识别 -n 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25915626/

相关文章:

python - 使用networkx计算有向图的传递闭包

python - 检查是否设置了 argparse 可选参数

python - Pytest Django 发现 Fixture 但在运行时显示 "Not Found"

python - 为什么我们需要在 sklearn kfold.split() 中给出 y?

python - 如何测试(使用 unittest)Django View 的 HTML 输出?

python - 合并两组非常相似的pytests

python - 无法在我扭曲的网络资源中测试 reactor.callInThread

python - 如何修改 pytest 参数?

python - 没有输出,即使有 `py.test -s`

python - 是否可以将 xdist 网关编号打印到 stdout 中的每一行?