python nosetests AssertionError:None!='hmmm ...'

时间:2016-06-10 01:12:42

标签: python-2.7 nose assertion

下面是我试图运行的测试:

    def test_hmm_method_returns_hmm(self):
    #set_trace()
    assert_equals( orphan_elb_finder.hmm(), 'hmmm...')

当我运行代码时,我得到以下输出:

D:\dev\git_repos\platform-health\tests\unit\test_orphan_elb_finder>nosetests
.F
======================================================================
FAIL: test_hmm_method_returns_hmm (test_orphan_elb_finder.test_basic.BasicTestSuite)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\dev\git_repos\platform-health\tests\unit\test_orphan_elb_finder\test_basic.py", line 18, in test_hmm_method_returns_hmm
    self.assertEqual(orphan_elb_finder.hmm(), 'hmmm...')
AssertionError: None != 'hmmm...'
-------------------- >> begin captured stdout << ---------------------
hmmm...

--------------------- >> end captured stdout << ----------------------

----------------------------------------------------------------------
Ran 2 tests in 0.002s

FAILED (failures=1)

似乎是orphan_elb_finder.hmm结果为None。这很奇怪,因为当我取消注释set_trace并手动运行命令时,它会给我正确的输出:

-> assert_equals( orphan_elb_finder.hmm(), 'hmmm...')
(Pdb) orphan_elb_finder.hmm()
hmmm...

但是当我尝试在调试器中运行相同的断言时:

(Pdb) assert_equals(orphan_elb_finder.hmm(), 'hmmm...')
hmmm...
*** AssertionError: None != 'hmmm...'

我觉得它与使用stdout的方式有关但我对如何找到更多信息/解决这个问题有点失落。

以下是orphan_elb_finder方法:

# -*- coding: utf-8 -*-

def get_hmm():
    """Get a thought."""
    return 'hmmm...'

def hmm():
    """Contemplation..."""
    print get_hmm()

非常感谢任何帮助

更新:

因此,在Blckknght响应后,我试图调用get_hmm而不是hmm()。但是当我尝试调用该方法时,我得到以下错误

    assert_equals(orphan_elb_finder.get_hmm(), 'hmmm...')
AttributeError: 'module' object has no attribute 'get_hmm'

然后我尝试检查可用的方法

(Pdb) dir(orphan_elb_finder)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'core', 'hmm']

似乎以下模块由于某种原因没有显示get_hmm()方法?

更新2:

了解发生了什么。在 init .py里面的orphan_elb_finder包里面我有

from .core import hmm

将其更改为

from .core import get_hmm

它似乎有效。不知怎的,虽然我认为包构造的作者将get_hmm缩进为私有方法。不确定我是如何测试它的,如果是这样的情况,因为get_hmm返回None?

1 个答案:

答案 0 :(得分:2)

hmm方法不同,get_hmm方法没有return语句。它是print字符串"hmmm...",但返回None

比较调用get_hmm()和调用hmm()。前者将使用引号打印'hmmm...'。这是因为它返回了字符串,而交互式控制台正在打印返回值的repr。相反,当您调用hmm()时,它会执行自己的打印(没有引号),然后返回None(未指定其他内容时的默认返回值)。交互式控制台会在repr时跳过打印出None的返回值,因此无需额外打印。

>>> get_hmm()
'hmmm...'
>>> hmm()
hmmm...