我正在尝试测试tensorflow程序。我正在使用参数化的py.test夹具设置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
参数在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
等等。有关更多信息和更多用法示例,请参阅插件文档。
问题内容: 我想依次运行每个选定的py.test项目任意次。 我没有看到任何标准的py.test机制来执行此操作。 我试图做到这一点。我修改了传入的项目列表,以多次指定每个项目。测试项目的第一次执行可以按预期工作,但是这似乎对我的代码造成了一些问题。 此外,我希望每次运行都具有唯一的测试项目对象,因为我在各种报告代码中都使用id(项目)作为键。不幸的是,我找不到任何py.test代码来复制测试项目
我想在测试套件中的每个测试之前和之后运行其他设置和拆卸检查。我看过赛程,但不确定它们是否是正确的方法。我需要在每次测试之前运行设置代码,并且需要在每次测试后运行拆卸检查。 我的用例是检查没有正确清理的代码:它会留下临时文件。在我的设置中,我将检查文件,在拆解中我也会检查文件。如果有额外的文件,我希望测试失败。
问题内容: 我想开始为我的Python代码编写单元测试,而py.test框架听起来比Python捆绑的unittest更好。因此,我在项目中添加了“ tests”目录,并在其中添加了test_sample.py。现在,我想配置PyCharm以运行“ tests”目录中的所有测试。 据称,PyCharm在其测试运行程序中支持py.test。您应该能够创建运行/调试配置来运行测试,并且PyCharm据
例如: 数据提供程序1:dataA 数据提供程序2:dataB,dataC 数据提供程序3:dataD、dataE、dataG 所以我的测试需要一个数据提供者。但是我希望它根据测试组为不同的测试类型选择不同的数据提供者。例如,如果我正在运行“smoke”组,那么我希望使用数据提供者1;如果我正在运行“sanity”组,那么我希望运行数据提供者1和数据提供者2;如果我正在运行“regression”
如何在将GUI保持为活动状态而不是Hibernate/等待状态的同时延迟进程或创建队列?
问题内容: 我正在使用py.test运行一组测试。他们通过了。pp!但我收到此消息: 我应该如何追踪其来源?(我不是直接使用线程,而是在使用gevent。) 问题答案: 我观察到类似的问题,并决定确切地了解发生了什么-让我描述一下我的发现。我希望有人会觉得有用。 短篇故事 它确实与猴子修补模块有关。实际上,通过在猴子修补线程之前导入线程模块,我可以轻松触发异常。以下两行就足够了: 执行时,它会吐出