Python 鼻子测试(实际上错误来自 Mox)每行打印一个字符的错误(带有行号!)

发布于 2024-11-09 08:10:22 字数 1576 浏览 0 评论 0原文

我最近开始使用 Nose 进行单元测试。它非常好,只是有时发生错误时它会以一种非常奇怪的方式打印出错误信息。它将其分成每行 1 个字符,然后将其与行号一起打印出来。有谁知道如何解决这个问题?

....F...............
======================================================================
FAIL: accounts.tests.testaccountserver.test_create_account_writes_an_account_to_the_store
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tom/envs/myappshare/lib/python2.7/site-packages/nose/case.py", line 187, in runTest
    self.test(*self.arg)
  File "/media/Shared/Dropbox/jobs/myapp/myappshare/src/accounts/tests/testaccountserver.py", line 102, in test_create_account_writes_an_account_to_the_store
    mox.VerifyAll()
  File "/home/tom/envs/myappshare/lib/python2.7/site-packages/mox.py", line 286, in VerifyAll
    mock_obj._Verify()
  File "/home/tom/envs/myappshare/lib/python2.7/site-packages/mox.py", line 506, in _Verify
    raise ExpectedMethodCallsError(self._expected_calls_queue)
ExpectedMethodCallsError: Verify: Expected methods never called:
  0.  V
  1.  e
  2.  r
  3.  i
  4.  f
  5.  y
  6.  :
  7.   
  8.  E
  9.  x
 10.  p
 11.  e
 12.  c
 13.  t
 14.  e
 15.  d
 16.   
 17.  m
 18.  e       

以此类推 1346 行!

编辑:

它不会让我在 8 小时内回答我自己的问题,所以我正在编辑我找到的解决方案:

正如 Aaron Digulla 指出的那样,问题不在鼻子中,而是在 Mox 中(我用它来模拟对象) )。

将此行添加到 mox.py 中 ExpectedMethodCallsError 的 str 方法的顶部修复了问题(或者无论如何都是这种症状):

if isinstance(self._expected_methods, str):
  self._expected_methods = self._expected_methods.split("\n")

I've recently started using Nose for my unit tests. It's pretty good except that sometimes when an error occurs it prints out the error information in a really weird way. It splits it up into 1 character per line then prints it out with line numbers. Does anyone have any idea how to fix this?

....F...............
======================================================================
FAIL: accounts.tests.testaccountserver.test_create_account_writes_an_account_to_the_store
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tom/envs/myappshare/lib/python2.7/site-packages/nose/case.py", line 187, in runTest
    self.test(*self.arg)
  File "/media/Shared/Dropbox/jobs/myapp/myappshare/src/accounts/tests/testaccountserver.py", line 102, in test_create_account_writes_an_account_to_the_store
    mox.VerifyAll()
  File "/home/tom/envs/myappshare/lib/python2.7/site-packages/mox.py", line 286, in VerifyAll
    mock_obj._Verify()
  File "/home/tom/envs/myappshare/lib/python2.7/site-packages/mox.py", line 506, in _Verify
    raise ExpectedMethodCallsError(self._expected_calls_queue)
ExpectedMethodCallsError: Verify: Expected methods never called:
  0.  V
  1.  e
  2.  r
  3.  i
  4.  f
  5.  y
  6.  :
  7.   
  8.  E
  9.  x
 10.  p
 11.  e
 12.  c
 13.  t
 14.  e
 15.  d
 16.   
 17.  m
 18.  e       

And so on for 1346 lines!

EDIT:

It won't let me answer my own question for 8 hours, so I'm editing in the solution I found:

As Aaron Digulla points out the problem is not in nose but in Mox (which I'm using to Mock objects).

Adding this line to the top of str method of ExpectedMethodCallsError in mox.py fixes the problem (or this symptom anyway):

if isinstance(self._expected_methods, str):
  self._expected_methods = self._expected_methods.split("\n")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悍妇囚夫 2024-11-16 08:10:22

正如 Aaron Digulla 指出的那样,问题不在于鼻子,而在于 Mox(我用它来模拟对象)。

将此行添加到 mox.py 中 ExpectedMethodCallsError 的 str 方法的顶部修复了问题(或者无论如何都是这种症状):

if isinstance(self._expected_methods, basestring):
  self._expected_methods = self._expected_methods.split("\n")

As Aaron Digulla points out the problem is not in nose but in Mox (which I'm using to Mock objects).

Adding this line to the top of str method of ExpectedMethodCallsError in mox.py fixes the problem (or this symptom anyway):

if isinstance(self._expected_methods, basestring):
  self._expected_methods = self._expected_methods.split("\n")
妥活 2024-11-16 08:10:22

ExpectedMethodCallsError__repr____str__ 方法中似乎存在错误。

或者在 mock_obj 中注册预期方法的代码中。

There seems to be an error in ExpectedMethodCallsError's __repr__ or __str__ method.

Or in the code which registers the expected methods in mock_obj.

风筝有风,海豚有海 2024-11-16 08:10:22

我无法直接解决你的问题,但我知道如何生成类似的问题。似乎鼻子会迭代堆栈跟踪:

for line in lines:
   print "%s" % line

如果由于某种原因,变量 lines 是一个字符串,则将迭代该字符串而不是行(导致每个字符打印一行,就像您的情况一样) 。

至于问题是什么:我不知道:)

I can't directly solve your problem, but I know how to generate similar. It seems that nose iterates through the stack trace:

for line in lines:
   print "%s" % line

If for some reason, variable lines is a string, the string will be iterated instead of lines (resulting in one line per character printing just like in your case).

As of what is the problem: I don't know :)

做个ˇ局外人 2024-11-16 08:10:22

在我的系统上,我发现字符串被作为 unicode 传入,一个额外的测试用例将涵盖这一点:

if (isinstance(self._expected_methods, str) or 
    isinstance(self._expected_methods, unicode)):
    self._expected_methods = self._expected_methods.split("\n")

On my system I found the string was being passed in as unicode, an additional test case will cover this:

if (isinstance(self._expected_methods, str) or 
    isinstance(self._expected_methods, unicode)):
    self._expected_methods = self._expected_methods.split("\n")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文