簡體   English   中英

在不同的進程中運行py.test測試

[英]Run py.test test in different process

我正在嘗試測試tensorflow程序。 我正在使用參數化py.test fixture設置tensorflow會話:

@pytest.fixture(scope="session", params=configuration)
def session(request):
    if request.param == 'tensorflow':
        return tf.Session()
    elif request.param == 'tensorflow-eager':
        tfe.enable_eager_execution()
        return tf.Session()
    elif ...

Tensorflow具有全局狀態,因此多次測試啟動可能會污染它。 例如,啟用后無法禁用急切執行。 有沒有辦法指示py.test為每個測試創建一個新進程? 或者使用參數化夾具配置測試環境的其他方法? 用法示例:

@pytest.mark.parametrize("bias_type", ['variable', 'ndarray', 'list', 'tuple'])
@pytest.mark.parametrize("kernel_type", ['variable', 'ndarray', 'list', 'tuple'])
@pytest.mark.parametrize("input_type", ['variable', 'ndarray', 'list', 'tuple'])
def test_convolution(session, input_type, kernel_type, bias_type):
    ...

正如評論中所建議的那樣,使用pytest-xdist將是解決方案。 該插件設計用於並行或分布式執行測試(甚至可以執行多平台執行),但非常適合在單獨的進程中提供運行每個測試的請求 - 您可以使用--forked參數實現此--forked

放棄

--forked參數不適用於Windows,因為Windows不支持fork-exec模型,也不提供fork()任何替換。

快速演示

讓我們定義一個夾具,它將在運行每個測試之前嘗試打開急切的執行:

from tensorflow.contrib.eager.python import tfe

import pytest


@pytest.fixture(scope='function', autouse=True)
def eager(request):
    tfe.enable_eager_execution()

這個燈具顯然會失敗所有測試,但第一個測試因為急切執行只能轉一次。 通過一些虛擬測試:

def test_spam():
    assert True

def test_eggs():
    assert True

def test_bacon():
    assert True

運行普通pytest失敗按預期方式:

$ pytest -v
============================== test session starts ================================
platform darwin -- Python 3.6.3, pytest-3.3.1, py-1.5.2, pluggy-0.6.0 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-48234032, inifile:
plugins: forked-0.2, mock-1.6.3, hypothesis-3.44.4
collected 3 items

test_spam.py::test_spam PASSED                                                [ 33%]
test_spam.py::test_eggs ERROR                                                 [ 66%]
test_spam.py::test_bacon ERROR                                                [100%]

...
E       ValueError: Do not call tfe.enable_eager_execution more than once in the
same process. Note eager-mode methods such as tfe.run() also call 
tfe.enable_eager_execution.
...

現在安裝pytest-xdist

$ pip install pytest-xdist

並重新運行測試:

$ pytest -v --forked
============================== test session starts ================================
platform darwin -- Python 3.6.3, pytest-3.3.1, py-1.5.2, pluggy-0.6.0 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-48234032, inifile:
plugins: forked-0.2, xdist-1.22.0, mock-1.6.3, hypothesis-3.44.4
collected 3 items

test_spam.py::test_spam PASSED                                                [ 33%]
test_spam.py::test_eggs PASSED                                                [ 66%]
test_spam.py::test_bacon PASSED                                               [100%]

============================= 3 passed in 6.09 seconds ============================

測試仍然按順序運行,但每個都在自己的子進程中運行,因此它們都沒有失敗。

現在您可以開始嘗試並行執行,例如

$ pytest -v --forked --numprocesses=auto

有關更多信息和更多用法示例,請參閱插件文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM