5

project tree:

.
|-- bar.py
`-- test
    |-- __init__.py
    `-- test_bar.py

bar.py:

def dumb_true():
    return True

tests/test_bar.py:

import bar

def test_bar_true():
        assert bar.dumb_true()

I can run nosetests from inside the project or its test directory. If I add an empty __init__.py to the project folder, however, I can no longer run nosetests from inside the test directory, which doesn't make any sense to me.

.
|-- bar.py
|-- __init__.py  <-- new, empty file, ruining everything
`-- test
    |-- __init__.py
    `-- test_bar.py

Can anyone explain to me what's going on here?

I've read extensively on this topic - through nose documentation/man pages and all over the internet; but it looks very confusing to me how this all resolves!

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Gary Fixler
  • 5,632
  • 2
  • 23
  • 39
  • what command are you using to run nose? Also, it might be more helpful to use absolute imports. If the directory that bar.py located in also named bar ? – Greg Aug 12 '13 at 07:14
  • Yeah, I know about absolute imports, but I don't like the leaves knowing the names of the branches they're inside of. I'm developing, so names are a bit liquid, so the name of the folder these things are in isn't reliable. I'm just running fairly stock `nosetests` calls. – Gary Fixler Aug 14 '13 at 12:19
  • Absolute imports are more accepted as standard practice in the python community than relative ones. Are you adding arguments to nosetests to specify the path of the tests? If so what path are you specifying? – Greg Aug 15 '13 at 09:26

1 Answers1

3

Looks like your question was answered here.

You've got an __init__.py in your top level directory. That makes it a package. If you remove it, your nosetests should work.

If you don't remove it, you'll have to change your import to import dir.foo, where dir is the name of your directory.

Community
  • 1
  • 1
Rooks103
  • 62
  • 6