Search code examples
pythonnose

Python: Nose not picking up new test


I have just added a unit test to one of my Python modules that nose is refusing to pick up. The test looks like this:

class TestMargin(unittest.TestCase):

    def setUp(self):
        super().setUp()

    def test_margin(self):
        self.assertTrue(False)

I would expect this test to fail but it seems it's not even picked up. I am running nose with the --all-modules flag, and tests in other modules are indeed being picked up so it doesn't seem to be a generic problem with nose. I have checked the documentation of nose and can't see any good reason why this test shouldn't work...

EDIT: I fixed the problem by (somewhat haphazardly) adding an empty __init__.py at the level of the module. But why would this help?


Solution

  • Your test looks fine, but you have to place it in the right place in the right filename in order for nose to discover it.

    You can see the inner working of nose by running it with -vvv

    By default nose makes decision whether to descent into your directory structure by checking two things: is it a package? if yes, nose will keep looking into directory for more tests. And being a package in python means having __init__.py file. That is why adding __init__.py fixed the issue.

    If the directory is not a package, nose may still want to look into it, if it looks like a test directory, i.e. tests or if it looks like a source directory, i.e. src or lib. But I guess that was not the case.

    Have a look at nose usage documentation:

    Any python source file, directory or package that matches the testMatch regular expression (by default: (?:^|[b_.-])[Tt]est) will be collected as a test (or source for collection of tests). In addition, all other packages found in the working directory will be examined for python source files or directories that match testMatch. Package discovery descends all the way down the tree, so package.tests and package.sub.tests and package.sub.sub2.tests will all be collected.

    Also, nose is just a bunch of python files, so you can always look into code to see how it discovers tests. In this case, you may want to look into wantDirectory() method in selector.py