标签: pytest-xdist

如何修复“pytest:错误:无法识别的参数:-n”

您好,我正在尝试在天蓝色管道上并行运行我的测试(pytest-xdist)。到目前为止,测试运行得非常好。突然,pytest 抛出一个奇怪的错误,提示“无法识别的参数”。

文件名:integration_test.py 使用的命令:pytest -n 5 --tb=shortintegration_test.py -v -s --> 并行运行 5 个测试测试总数:57 版本:pytest==6.2.5 pytest- xdist==2.3.0 甚至尝试过这两个模块的最新版本。

错误:错误:用法:pytest [选项] [file_or_dir] [file_or_dir] [...] pytest:错误:无法识别的参数:-nintegration_test.py

我怎样才能克服这个错误?

pytest pytest-xdist

13
推荐指数
1
解决办法
2万
查看次数

pytest 与会话范围的固定装置和 asyncio 相关的问题

我有多个测试文件,每个文件都有一个异步夹具,如下所示:


@pytest.fixture(scope="module")
def event_loop(request):
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()


@pytest.fixture(scope="module")
async def some_fixture():
    return await make_fixture()
Run Code Online (Sandbox Code Playgroud)

我正在使用 xdist 进行并行化。另外我有这个装饰器:

@toolz.curry
def throttle(limit, f):
    semaphore = asyncio.Semaphore(limit)

    @functools.wraps(f)
    async def wrapped(*args, **kwargs):
        async with semaphore:
            return await f(*args, **kwargs)

    return wrapped
Run Code Online (Sandbox Code Playgroud)

我有一个函数使用它:

@throttle(10)
def f():
    ...
Run Code Online (Sandbox Code Playgroud)

现在f正在从多个测试文件调用,并且我收到一个异常,告诉我无法使用不同事件循环中的信号量。

我尝试转向会话级事件循环装置:



@pytest.fixture(scope="session", autouse=True)
def event_loop(request):
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()

Run Code Online (Sandbox Code Playgroud)

但这只给了我:

ScopeMismatch:您尝试使用“模块”范围请求对象访问“函数”范围固定装置“event_loop”,涉及工厂

是否有可能让 xdist + 异步装置 + 信号量一起工作?

python semaphore pytest python-asyncio pytest-xdist

8
推荐指数
1
解决办法
3234
查看次数

Pytest:使用 pytest-xdist 对所有工作人员运行一次拆卸

我有一个 pytest 固定装置,只需在所有 pytest 工作人员中运行一次。

@pytest.fixture(scope="session")
@shared  # this will call setup once for all processes
def cache(request):
    acc = Account(id=10)
    acc.create()
    request.addfinilizer(acc.delete)
    return acc


def shared(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        request = kwargs['request']
        root = request.config._tmp_path_factory.getbasetemp().parent
        filepath = root / "shared"

        with filelock.FileLock(f'{filepath}.lock'):
            if filepath.is_file():
                result = json.loads(filepath.read_text())
            else:
                result = func(*args, **kwargs)
                filepath.write_text(json.dumps(result.id))

        return result
    return wrapper
Run Code Online (Sandbox Code Playgroud)

我使用https://pytest-xdist.readthedocs.io/en/latest/how-to.html?highlight=only%20once#making-session-scoped-fixtures-execute-only-once中的解决方案,效果很好对于 pytestsetup部分,但该teardown部分在每个 pytest 进程中都会被调用。

是否可以锁定pytest-xdist拆卸以在所有 pytest 会话完成后仅运行一次?我想为所有工人运行一次拆卸。

python pytest pytest-xdist

8
推荐指数
1
解决办法
1873
查看次数

pytest-xdist 因 pytest-cov 错误而崩溃

我试图通过主节点的 ssh 在远程计算机上对我的包执行测试。两个节点都安装了相同版本的软件包。

我正在这样进行测试

pytest -d --tx ssh=ubuntu//python=python3  --rsyncdir /home/ubuntu/pkg/ /home/ubuntu/pkg -n 7
Run Code Online (Sandbox Code Playgroud)

运行此程序时,我收到以下错误,

------------------------------ coverage ------------------------------
---------------------- coverage: failed workers ----------------------
The following workers failed to return coverage data, ensure that pytest-cov is installed on these workers.
gw0
gw1
gw2
gw3
gw4
gw5
gw6
Coverage XML written to file coverage.xml
Run Code Online (Sandbox Code Playgroud)

我已确保工作节点中安装了覆盖范围。

coverage==6.2
pytest-cov==3.0.0
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它仍然失败。

我还注意到,由于某种原因,代码文件尚未在工作计算机中同步。

我试图了解这里出了什么问题以及如何解决这个问题。

python unit-testing pytest python-3.x pytest-xdist

7
推荐指数
1
解决办法
969
查看次数

为什么使用 pytest-xdist 时 pytest_sessionstart 挂钩会运行多次?

我尝试使用 pytest-xdist 在我的硒网格上并行运行 pytest。在使用 pytest-xdist 之前,我在所有测试开始使用 pytest_sessionstart 挂钩之前执行了一次设置。它工作得很好。这将是第一个运行的事情,在它完成之前不会开始任何测试。一旦我尝试使用 pytest-xdist,所有会话范围的挂钩(例如 pytest_sessionstart 和 pytest_sessionfinish)都会被执行多次。我不明白或做错了什么?

pytest python-3.x xdist pytest-xdist

6
推荐指数
0
解决办法
1466
查看次数

如何运行一次设置(夹具)然后并行测试

我使用 pytest-xdist 并行运行测试,但我的套件设置非常庞大且笨重,我希望避免在每个测试执行中运行多次。
当我使用 pytest-xdist 并行运行所有测试时,我遇到了这样的问题:我的套件设置(固定装置)在每次测试执行之前运行,这增加了所有测试用例的执行时间。
有什么方法可以避免在 pytest 并行执行中每次测试执行之前执行固定装置吗?

python pytest pytest-parallel pytest-xdist

6
推荐指数
1
解决办法
3232
查看次数

并行执行的 Pytest 实时日志记录 - 可能吗?

我有一个运行的测试套件

python3 -mpytest --log-cli-level=DEBUG ...
Run Code Online (Sandbox Code Playgroud)

在构建服务器上。如果测试因某种原因卡住或缓慢(测试使用外部资源),实时日志可用于排除故障。

为了加快速度,可以使用例如运行它们

python3 -mpytest -n 4 --log-cli-level=DEBUG ...
Run Code Online (Sandbox Code Playgroud)

有四个并行测试运行程序。加速几乎与进程数量成线性关系,这很好,但不幸的是父进程吞下了所有实时日志。如果测试失败,我会获取捕获的日志,但我还需要实时日志来了解实时发生的情况。我知道所有四个并行运行的输出都会混合,这很好。目的是让提交者检查构建服务器输出并大致了解发生了什么。

我目前正在使用 pytest-xdist,但没有使用它的任何更高级的功能(只是多处理)。

pytest pytest-xdist

6
推荐指数
0
解决办法
769
查看次数

pytest-xdist IOError:无法发送到 <Channel id=1 close>

我使用 pytest 和 pytest-xdist 来测试 django 应用程序,当我运行时py.test -n 1出现错误:

Test session starts (platform: linux2, Python 2.7.12, pytest 2.9.2, pytest-sugar 0.7.1)
django settings: icbase.settings (from ini file)
rootdir: /home/xsy/icgoo_git/datacenter3, inifile: pytest.ini
plugins: sugar-0.7.1, xdist-1.15.0, cov-2.3.1, django-2.9.1
gw0 okINTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/home/xsy/.virtualenvs/dc3/lib/python2.7/site-packages/_pytest/main.py", line 94, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/home/xsy/.virtualenvs/dc3/lib/python2.7/site-packages/_pytest/main.py", line 125, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/home/xsy/.virtualenvs/dc3/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, …
Run Code Online (Sandbox Code Playgroud)

python django pytest pytest-xdist

5
推荐指数
1
解决办法
1798
查看次数

Pytest/xdist 中的序列化问题

Replacing crashed worker gw0
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 191, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 246, in _main
INTERNALERROR>     config.hook.pytest_collection(session=session)
INTERNALERROR>   File "/Users/Library/Python/3.7/lib/python/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/Users/Library/Python/3.7/lib/python/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/Users/Library/Python/3.7/lib/python/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/Users/Library/Python/3.7/lib/python/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result() …
Run Code Online (Sandbox Code Playgroud)

pytest python-3.x xdist pytest-xdist

5
推荐指数
0
解决办法
1207
查看次数

Pytest Xdist 不同测试收集错误

第一次在这里发帖,过去两天我尝试搜索 xdist 问题的解决方案。当我尝试以 n=2 或更高的值运行时,有时(我说有时是因为它随机工作)会出现以下错误:

Different tests were collected between gw1 and gw0. The difference is:
--- gw1

+++ gw0

@@ -1,2 +1,2 @@

+test_Sign_Up.py::test_sign_up[chrome]  test_Sign_Up.py::test_sign_up[firefox]
-test_Sign_Up.py::test_sign_up[chrome]
Run Code Online (Sandbox Code Playgroud)

抱歉,如果这是一个很容易解决的问题,但在看了很多其他帖子后,我个人觉得我没有找到答案。如果需要更多信息,请告诉我,我会尽快回复!谢谢你!

编辑:设置

Python 3.5.3
Pytest 3.1.2
Xdist 1.17.1
Run Code Online (Sandbox Code Playgroud)

python pytest xdist pytest-xdist

3
推荐指数
1
解决办法
3786
查看次数

如何告诉 pytest-xdist 依次从一个文件夹运行测试,其余的并行运行?

想象一下,我有test/unit/...哪些可以安全地并行运行,test/functional/...哪些还不能并行运行。

有没有一种简单的方法来说服 pytestfunctional依次运行它们?考虑到我们正在谈论大量测试,因此更改每个测试功能/方法会非常嘈杂。

此时我们使用标记过滤器运行测试,因此我们主要将它们分开运行。不过,我正在寻找一种解决方案,以消除将它们分开运行的需要。

python pytest pytest-xdist

3
推荐指数
1
解决办法
694
查看次数