Anne
Anne

Reputation: 7022

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?

Upvotes: 4

Views: 2040

Answers (2)

shubhparekh
shubhparekh

Reputation: 41

If your naming is proper and still file is not getting picked one of the possible reasons is your test file has executable permission on it -rwxrwxr-x. Modify the permissions to -rw-rw-r--. Python Nose does not pick files with executable permissions.

In case executable flag is added by mistake to version control remove permissions as follows

Git

Find permissions via

git ls-files --stage | grep file_name

Remove execute permission and then commit

git update-index --chmod=-x file_path

SVN

If svn Properties are present can be listed via

svn proplist test_file_path

Property Deletion

svn propdel svn:executable test_file_path

Upvotes: 1

Oleksiy
Oleksiy

Reputation: 6567

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

Upvotes: 6

Related Questions