Justin S
Justin S

Reputation: 1469

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

below is the test I am trying to run:

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

When I run the code I get the following output:

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)

It seems to be that orphan_elb_finder.hmm results to None. Which is weird because when I uncomment the set_trace and run the command manually it gives me the correct output:

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

But when I try and run the same assertion in the debugger:

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

I have a feeling that it has something to do with the way stdout is used but I'm a little bit lost as to how to find out more information / fix this problem.

Below is the orphan_elb_finder methods:

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

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

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

Any help would be greatly appreciated

UPDATE:

So following Blckknght response I have tried to call get_hmm instead of hmm(). But when I try call the method I get the below error

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

Then I try and check available methods

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

It seems that the below module does not reveal the get_hmm() method for some reason?

UPDATE 2:

Found out what was going on. Inside my orphan_elb_finder package inside init.py I had

from .core import hmm

changed it to

from .core import get_hmm

and it seemed to work. Somehow though I think the author of the package construction indented get_hmm to be a private method. Not sure How I would have tested it if that is the case seeing as get_hmm returns None?

Upvotes: 1

Views: 6948

Answers (1)

Blckknght
Blckknght

Reputation: 104842

The hmm method, unlike the get_hmm method, does not have a return statement. It prints the string "hmmm...", but returns None.

Compare calling get_hmm() and calling hmm(). The former will print 'hmmm...' with the quotation marks. That's because it's returning the string, and the interactive console is printing the repr of the return value. In contrast, when you call hmm(), it does its own printing (with no quotation marks), then returns None (the default return value when nothing else is specified). The interactive console skips printing out the repr of the return value when it is None, so there's nothing extra printed.

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

Upvotes: 3

Related Questions