Nose unable to find tests in ubuntu

2019-01-16 10:53发布

Is there any reason why Nose wouldn't be able to find tests in Ubuntu 9.04?

I'm using nose 0.11.1 with python 2.5.4.
I can run tests only if I explicitly specify the filename. If I don't specify the filename it just says, 0 tests.

The same project runs tests fine on my Mac, so I'm quite stumped!

标签: python nose
7条回答
beautiful°
2楼-- · 2019-01-16 11:23

This behavior is almost certainly because your files are not named in accordance with nose's test matching behavior. From the nose docs:

nose collects tests automatically from python source files, directories and packages found in its working directory (which defaults to the current working directory). 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).

Emphasis was mine.

Some example names that would match:

  • TestFoo.py
  • Foo-Test.py
  • Foo_Test.py
  • Foo.Test.py (note that this one will try to import Foo, and will raise an exception if it cannot)

A name that looks like it would match, but actually does not:

  • FooTest.py

If you just rename your files you should be good to go.


Update: I wasn't able to tell from the details you've posted, but maybe your test directories are missing their __init__.py files?

... make sure that your “tests” directories are actually modules (they have an empty __init__.py file).

查看更多
Emotional °昔
3楼-- · 2019-01-16 11:26

Use the -all-modules and it will find all the tests.

nosetests --all-modules ./tests

查看更多
萌系小妹纸
4楼-- · 2019-01-16 11:27

Some what related, if you're running tests off of a directory i.e

nosetests ... tests/

where tests is the name of the folder with my tests, and have separate python test functions in one of the .py modules... Your functions have to start with 'test' for nosetests to recognize that as a test you want to run.

for example:

 def test_something():
    ...

nosetests will run this function when executed in this directory while

 def somethin_to_test():
    ...

would not.

查看更多
We Are One
5楼-- · 2019-01-16 11:38

After looking through the source of nose, specifically the selector.py file, if you look at what's happening,

https://github.com/nose-devs/nose/blob/master/nose/selector.py#L129

When checking if we wantFile, self.matches is called, which then does a regex search against the match, which is what you would have passed in as testMatch.

The problem occurs when you then check later down (and, throughout that file),

https://github.com/nose-devs/nose/blob/master/nose/selector.py#L152

It runs the very same type of checks again, against wantFunction.

This means, if you've got a different structure for your package, your container pyfile, and your actual test class / function, you'll have to create a crazy complicated regex to match that at every stage.

For me, when I learned this, I chose to prefix my package, container and test functions with a common bit, i.e.

setests ├── __init__.py ├── setest_area1.py └──── def setest_someblock(): ...

And then my nose command works like,

nose --testMatch="setest"

This then filters the way I expect it to work.

查看更多
Evening l夕情丶
6楼-- · 2019-01-16 11:39

I had the same problem. My tests ran just fine in Windows, but not in Ubuntu.

In Ubuntu, if you run:

nosetests -vv --collect-only

You'll probably see that it's skipping your test file because it's an executable: _Tools/LintControlFiles/test_HgLint.py is executable; skipped

In order to get nose to consider executables, run it like this:

nosetests --exe
查看更多
神经病院院长
7楼-- · 2019-01-16 11:42

I can confirm that as @david-wolever said, they cannot be executable on Ubuntu. Run

nosetests -vv --collect-only 

to see full details on which files were examined.

查看更多
登录 后发表回答