繁体   English   中英

带有xdist的py.test跳过-n> 1的所有测试

[英]py.test with xdist skipping all the tests with -n > 1

我的测试需要2分钟才能运行:

$ py.test
================================================= test session starts =================================================
platform linux2 -- Python 2.7.8 -- py-1.4.24 -- pytest-2.5.2
plugins: cov, xdist
collected 2249 items 

«lots of file names»
====================================== 2242 passed, 7 skipped in 120.01 seconds =======================================

…所以我想我会尝试xdist插件来并行运行它们。 所以我做了:

$ pip install pytest-xdist
$ py.test -n 2
================================================= test session starts =================================================
platform linux2 -- Python 2.7.8 -- py-1.4.24 -- pytest-2.5.2
plugins: cov, xdist
gw0 [2249] / gw1 [2249]
scheduling tests via LoadScheduling

==================================================  in 2.65 seconds ===================================================

2秒钟将是惊人的加速……尽管我认为没有进行任何测试-某些点会出现,不是吗? 但是,如果我只用一个进程就“并行”运行…

$ py.test -n 1
================================================= test session starts =================================================
platform linux2 -- Python 2.7.8 -- py-1.4.24 -- pytest-2.5.2
plugins: cov, xdist
gw0 [2249]
scheduling tests via LoadScheduling
....«lots and lots of dots»........
====================================== 2242 passed, 7 skipped in 122.27 seconds =======================================

…然后时间恢复正常。

如何使xdist插件实际运行测试?

更新:

布鲁诺·奥利维拉(Bruno Oliveira)问题的答案:

$ py.test -n 4 -vv
============================= test session starts ==============================
platform linux2 -- Python 2.7.8 -- py-1.4.24 -- pytest-2.5.2 -- /home/liori/proj/.ve/bin/python2
plugins: cov, xdist
[gw0] linux2 Python 2.7.8 cwd: /home/liori/proj/src
[gw1] linux2 Python 2.7.8 cwd: /home/liori/proj/src
[gw2] linux2 Python 2.7.8 cwd: /home/liori/proj/src
[gw3] linux2 Python 2.7.8 cwd: /home/liori/proj/src
[gw0] Python 2.7.8 (default, Aug 23 2014, 21:00:50)  -- [GCC 4.9.1]
[gw1] Python 2.7.8 (default, Aug 23 2014, 21:00:50)  -- [GCC 4.9.1]
[gw2] Python 2.7.8 (default, Aug 23 2014, 21:00:50)  -- [GCC 4.9.1]
[gw3] Python 2.7.8 (default, Aug 23 2014, 21:00:50)  -- [GCC 4.9.1]
gw0 [2254] / gw1 [2254] / gw2 [2254] / gw3 [2254]
scheduling tests via LoadScheduling

===============================  in 4.63 seconds ===============================

对我来说,我注意到类似的问题:)

您的测试是否以某种方式随机参数化? 如果是,请看看py.test与xdist不在执行参数化为随机值的测试

在您和我的情况下,它实际上都没有跳过(如果确实被跳过,则摘要中您将跳过X)

正如Marek所建议的那样,我尚未使用随机值对测试进行参数化。 但是,他的建议促使我去检验另一个假设,这似乎是正确的: xdist要求测试的参数化始终以相同的顺序生成。

在我的特定情况下,我通过遍历一set字符串由参数化生成。 但是,此迭代取决于字符串哈希到的特定值,并且每个进程的这些值可能不同。 因此,尽管我总是生成完全相同的参数,但它们的顺序却不同。

一个显示问题的简单测试用例:

import pytest

my_names = {'john', 'kate', 'alfred', 'paul', 'mary'}

@pytest.mark.parametrize('name', list(my_names), ids=list(my_names))
def test_is_name_short(name):
    assert len(name) < 7

使用PYTHONHASHSEED=random py.test -n 4运行,以确保触发字符串的随机散列。

一个简单的解决方法是对测试强制执行特定的排序,例如通过按一些参数对它们进行排序:

my_names = sorted(my_names)

我已经向py.test的bugtracker提交了一个建议, 使xdist排序参数化以进行比较,以避免出现此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM