- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试编写一个 pytest 插件来自定义特定异常的外观 - 更具体地说,模拟异常(预期调用的方法没有被调用等),因为在那些的回溯中有很多无用的噪音异常(exception)。
这是我到目前为止所得到的,它有效,但非常hacky:
import pytest
import flexmock
@pytest.hookimpl()
def pytest_exception_interact(node, call, report):
exc_type = call.excinfo.type
if exc_type == flexmock.MethodCallError:
entry = report.longrepr.reprtraceback.reprentries[-1]
entry.style = 'short'
entry.lines = [entry.lines[-1]]
report.longrepr.reprtraceback.reprentries = [entry]
我认为我使用 hookimpl
做正确的事情并使用简单的 if 语句检查异常类型。
我尝试用一个简单的字符串替换 report.longrepr
,这也有效,但后来我失去了格式(终端中的颜色)。
作为我想要缩短的输出类型的示例,这里是一个模拟断言失败:
=================================== FAILURES ====================================
_______________________ test_session_calls_remote_client ________________________
def test_session_calls_remote_client():
remote_client = mock.Mock()
session = _make_session(remote_client)
session.connect()
remote_client.connect.assert_called_once_with()
session.run_action('asdf')
> remote_client.run_action.assert_called_once_with('asdff')
tests/unit/executor/remote_test.py:22:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/opt/python-3.6.3/lib/python3.6/unittest/mock.py:825: in assert_called_once_with
return self.assert_called_with(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_mock_self = <Mock name='mock.run_action' id='139987553103944'>
args = ('asdff',), kwargs = {}, expected = (('asdff',), {})
_error_message = <function NonCallableMock.assert_called_with.<locals>._error_message at 0x7f51646269d8>
actual = call('asdf'), cause = None
def assert_called_with(_mock_self, *args, **kwargs):
"""assert that the mock was called with the specified arguments.
Raises an AssertionError if the args and keyword args passed in are
different to the last call to the mock."""
self = _mock_self
if self.call_args is None:
expected = self._format_mock_call_signature(args, kwargs)
raise AssertionError('Expected call: %s\nNot called' % (expected,))
def _error_message():
msg = self._format_mock_failure_message(args, kwargs)
return msg
expected = self._call_matcher((args, kwargs))
actual = self._call_matcher(self.call_args)
if expected != actual:
cause = expected if isinstance(expected, Exception) else None
> raise AssertionError(_error_message()) from cause
E AssertionError: Expected call: run_action('asdff')
E Actual call: run_action('asdf')
/opt/python-3.6.3/lib/python3.6/unittest/mock.py:814: AssertionError
====================== 1 failed, 30 passed in 0.28 seconds ======================
最佳答案
如果您的目标是使堆栈跟踪更易于阅读,那么您可以使用以下代码块来输出自定义错误消息。此自定义错误消息显示在堆栈跟踪的末尾,因此您无需向上滚动:
with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"):
pass
--> Failed: Expecting ZeroDivisionError
来源:pytest's documentation .因此,您可以通过 grep 之类的方式将 pytest 的输出通过管道传输并过滤掉堆栈跟踪中无用的部分,而不是制作插件。
根据我在文档中阅读的内容,您正在使用正确的 pytest 装饰器和 Hook 函数 (pytest_exception_interact
)。但是,可能不需要对错误进行类型检查。 This section of the documentation表示“只有在引发不是内部异常(如 skip.Exception)的异常时才调用此钩子(Hook)。”
关于python - 为 pytest 中的特定异常自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36521111/
我需要在整体超时的情况下停止测试用例,而不是在测试用例级别。 所以如果让我说我有 300 个测试用例,我想超时,总时间为 300 秒。 有没有办法做到这一点? 用于运行 pytest 的示例命令 py
我会默认使用一些参数( -n 2 )运行 pytest 但如果我只输入 pytest ... ,我不希望默认使用该参数直接运行pytest。这可能吗? 如果我包括这个: [pytest] addopt
给定以下模型: import pytest class DummyFile(pytest.File): def collect(self): yield DummyItem(s
对于 pytest,我正在使用库 pytest-dependency 设置依赖项.我还为这些测试添加了标记。这是一个 ECM: # test_test.py import pytest @pytest
我想使用逻辑来控制我的测试的顺序,这将在它们已经运行时动态重新排序它们。 我的用例是这样的:我正在使用 xdist 并行化我的测试,并且每个测试都使用来自公共(public)和有限池的外部资源。一些测
我需要标记要跳过的某些测试。但是,有些测试是参数化的,我只需要能够跳过某些场景。 我使用 py.test -m "hermes_only" 调用测试或 py.test -m "not hermes_o
问题是我给定的 fixture 函数具有外部依赖性,这会导致“错误”(例如无法访问的网络/资源不足等)。 我想跳过 fixture ,然后跳过任何依赖于该 fixture 的测试。 做这样的事情是行不
我正在试用 pytest首次。我如何抑制发出的关于我的代码所依赖的其他人的代码的警告而不抑制关于我自己的代码的警告? 现在我的 pytest.ini 中有这个所以我不必看到 pytest 警告我关于
我试图跳过依赖于命令行参数值的特定测试。我尝试使用 pytest.config.getoption("--some-custom-argument") 获取参数值就像这里描述的一样 related q
我目前使用的是 python 3.5.1 和 3.6 以及最新版本的 pytest。当使用参数化测试运行 pytest 时,我希望任何失败的测试仅显示失败的测试,而不是参数化测试的所有设置。 解释一下
在我的测试套件中,我有一些数据生成装置,用于许多参数化测试。其中一些测试希望这些装置在每个 session 中只运行一次,而另一些则需要它们运行每个功能。例如,我可能有一个类似于: @pytest.f
我想在运行时获取测试名称和测试结果。 我有 setup和 tearDown我的脚本中的方法。在 setup ,我需要获取测试名称,并在 tearDown我需要得到测试结果和测试执行时间。 有没有办法我
有没有办法在 PyTest fixture 中定义标记? 当我指定 -m "not slow" 时,我试图禁用慢速测试在 pytest 中。 我已经能够禁用单个测试,但不能禁用用于多个测试的 fixt
我最低限度地使用 pytest 作为针对工作中各种 API 产品的大型自动化集成测试的通用测试运行器,并且我一直在尝试寻找一个同样通用的拆卸函数示例,该函数在任何测试完成时运行,无论成功或失败。 我的
即使在写入管道时,如何强制 pytest 以颜色显示结果?似乎没有任何命令行选项可以这样做。 最佳答案 从 2.5.0 开始,py.test 有选项 --color=yes 从 2.7.0 开始,还应
作为一组更大的测试的一小部分,我有一套测试函数,我想在每个对象列表上运行。基本上,我有一组插件和一组“插件测试”。 天真地,我可以只列出一个带有插件参数的测试函数列表和一个插件列表,然后进行测试,我在
我想为 pytest-xdist 产生的每个子进程/网关创建一个单独的日志文件。是否有一种优雅的方法可以找出 pytest 当前所在的子进程/网关?我正在使用位于 conftest.py 的 sess
我的测试脚本如下 @pytest.fixture(scope="Module", Autouse="True") def setup_test(): ....................
我正在尝试像这样参数化我的类测试: @pytest.mark.parametrize('current_user', ["test_profile_premium", "test_profile_fr
我不明白如何正确运行一个简单的测试(功能文件和 python 文件) 与图书馆 pytest-bdd . 来自官方documentation ,我无法理解要发出什么命令来运行测试。 我尝试使用 pyt
我是一名优秀的程序员,十分优秀!