gpt4 book ai didi

python - Nosetests 断言错误输出格式

转载 作者:行者123 更新时间:2023-12-01 05:30:21 24 4
gpt4 key购买 nike

嗨,所以我想知道是否有办法修复nosetests断言失败的输出。我有一个名为“t.py”的简单脚本:

import unittest
from nose.tools import assert_equal

class x(unittest.TestCase):
"""
Testing
"""
def test(self):
assert_equal(1, 2)

如果我使用命令“nosetests t”运行它,则会得到以下结果。

AssertionError: 1 != 2
'1 != 2' = '%s != %s' % (safe_repr(1), safe_repr(2))
'1 != 2' = self._formatMessage('1 != 2', '1 != 2')
>> raise self.failureException('1 != 2')

而不是运行“python -m unittest t”时得到的输出

AssertionError: 1 != 2

有谁知道如何使 Nose 测试输出与单元测试输出匹配?当运行大量测试时,这会使结果变得困惑。

这是在 ubuntu 虚拟机内的 virtualenv 中使用 python 2.7.3,nose==1.3.0。

编辑:

我对 Nose 尝试提供更多信息没有意见。我有一个不断增长的测试套件,额外的信息可能会有所帮助。只是它提供了相当无用的信息。我真的不需要多次看到相同的信息。如果有多个测试,或者特别是如果有如下所示的附加消息,则很难看出问题:

assert_equal(1,2, "Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isn't it?")

然后我得到这个:

assert_equal(1,2, "Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?")
AssertionError: Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isn't it?
'1 != 2' = '%s != %s' % (safe_repr(1), safe_repr(2))
"Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?" = self._formatMessage("Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?", '1 != 2')
>> raise self.failureException("Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?")

谢谢!

最佳答案

您是否尝试过使用 self.assertEqual ,其中 self 是“unittest.TestCase”?

为此

import unittest
class x(unittest.TestCase):
def test_1(self):
self.assertEqual(1,2)

我得到这个输出

laptop:~/workspace/test$ nosetests j.py
F
======================================================================
FAIL: test_1 (j.x)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/workspace/test/j.py", line 4, in test_1
self.assertEqual(1,2)
AssertionError: 1 != 2

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

编辑:您可能还喜欢“nosetests -q, --quiet”,以减少冗长

关于python - Nosetests 断言错误输出格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20409666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com