问题标签 [assertraises]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
379 浏览

python - Misbehaving assertRaises

I have class and method as below:

I want to write a unit test that checks if the given parameters are correct type. When you type will be angry, throw a TypeError.

When given the correct type of test parameters should show the error, and it is not. Why? Please for help/some hint

0 投票
1 回答
38211 浏览

python - python单元测试中的assertRaises没有捕获异常

有人能告诉我为什么下面的单元测试在 test_bad 中的 ValueError 上失败,而不是用 assertRaises 捕获它并成功吗?我想我使用了正确的过程和语法,但 ValueError 没有被抓住。

我在 linux 机器上使用 Python 2.7.5。

这是代码……</p>

这是单元测试的输出:

0 投票
2 回答
909 浏览

python - 如何使 assertRaises() 在 Python 2.7 中打印传递的 *args、**kwargs?

assertRaises在这样的循环中使用:

问题是,每当测试失败时,输出如下所示:

如何让它打印出i测试失败时的值?像这样:

0 投票
2 回答
510 浏览

python - 如何测试龙卷风 read_message 没有什么可读的

我有一个 Tornado 聊天,我正在做一些测试,大多数客户端消息会从服务器生成回复,但其他人不得生成任何回复。

我设法用这段代码做到了,等待读取超时发生,有更好的方法吗?

测试结束时也有问题

0 投票
2 回答
3603 浏览

python - Python assertRaises 处理用户定义的异常

以下问题是由这篇文章中的讨论引发的。

假设有两个文件(foobar.pyfoobar_unittest.py)。文件foobar.py包含一个具有两个函数(foobar )的类( FooBar)。函数bar引发内置异常,函数foo引发用户定义异常。

.

foobar.py上运行单元测试时,为什么引发用户定义异常 ( foo ) 的函数无法通过测试?

0 投票
1 回答
183 浏览

python - unittest:wxpython的事件方法引发异常但assertRaises没有检测到它

我有一个 wxpython 对话框,单击 OK 按钮时会引发 TypeError 异常。我想用 unittest 测试异常的发生,但测试没有按预期工作。输出显示引发了异常。无论如何 unittest 通知测试失败:

这是我的代码的简化示例:

有人可以帮我找出我做错了什么吗?

0 投票
4 回答
3246 浏览

python - 在 Python 中为 `assertRaises()` 使用自定义失败消息?

Python 2.7 unittest 文档说:

所有的 assert 方法(除了assertRaises(), assertRaisesRegexp())都接受一个msg参数,如果指定,则用作失败时的错误消息

…但是如果我想为assertRaises()or指定错误消息assertRaisesRegexp()怎么办?

用例:在循环中测试各种值时,如果一个失败,我想知道哪一个:

如果其中任何一个失败,我会得到:

这对我找出哪个失败了没有太大帮助……如果我能提供一个像我一样的msg=论点就好了assertEqual()

(我当然可以将它们分解为单独的测试函数——但也许有很多我想测试的值,或者它需要一些缓慢/昂贵的设置,或者它是更长的功能测试的一部分)

如果我可以很容易地让它报告类似的内容,我会喜欢它:

——但这也不是一个足够重要的事情来保证重新实现/扩展assertRaises()并在我的测试中添加更多的代码。

0 投票
1 回答
2332 浏览

python - assertRaises: KeyError exception is not raised while doing unit test of a method

I am testing an exception using assertRaises, even though exception is raised, it is not detected by assertRaises

Here is the method under test:

#xA;

And the test code is:

#xA;

But it is not working as expected, getting following errors:

#xA;

What is wrong with unit test case?

0 投票
0 回答
1103 浏览

json - JSON 模式“oneOf”的验证失败 unittest assertRaisesRegex

在使用 Python3.6 unittest验证某些 JSON 时,在 JSON关键字下验证代码和不是的代码assertRaisesRegex之间生成的输出存在差异。oneOf我正在测试删除required属性。

问题是用户期望一种类型的错误消息,但收到另一种错误消息并assertRaisesRegex失败,因为它与正则表达式不匹配。

使用http://json-schema-validator.herokuapp.com来验证 JSON 我看到预期的实际错误消息已经嵌套得更深了。

我可以通过在测试属性时搜索新消息来解决这个问题oneOf,但想知道是否有更好的方法来处理与我的其他测试一致的这个问题,即对我期望的消息使用相同的格式?

下面是一些测试requiredJSON 属性被删除的输出示例。预期的消息格式标有=>意外的**

示例 1 ( herokuapp.com):required删除属性时(不在 下oneOf

示例 2 ( herokuapp.com):删除oneOf嵌套required属性时

在我使用jsonschema的单元测试中,消息不同,但问题是相同的。

示例 1 ( jsonschema):required删除属性时(不在 下oneOf

示例 2 ( jsonschema):删除oneOf嵌套required属性时

我正在验证这个架构

这是我的实例数据

这些是我的测试

0 投票
1 回答
7730 浏览

python - 单元测试中的自定义异常

我已经在其中创建了我的自定义异常errors.py

代码中其他地方的另一个函数会引发不同的实例,说明数据帧DataException中是否没有足够的pandas数据。我想用unittest它来确保它返回适当的异常及其相应的消息。

使用一个简单的例子,为什么这不起作用:

如此处所建议:Python assertRaises on user-defined exceptions

我收到此错误:

或者:

结果是:

为什么它不被识别DataExceptionException?为什么链接的stackoverflow问题答案在不提供第二个参数的情况下工作assertRaises