57

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!

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90
  • Exactly what command are you entering when you expect tests to be run? – Mark Rushakoff Sep 21 '09 at 22:03
  • "nosetests --with-gae" or just "nosetests". I've also tried "nosetests tests", where "tests" is the name of my test file directory. All commands work perfectly on the Mac, no nose configs were changed on either machine. I works in ubuntu if I import the test files in the __init__.py of the tests directory and run "nosetests tests". This isn't sustainable, though. – Sudhir Jonathan Oct 16 '09 at 18:48
  • 10
    Try running `nosetests` with a few `-v`'s… `-vv` has usually given me enough information to solve my problems. – David Wolever Oct 16 '09 at 19:00
  • Could you show us a bit of the directory structure? From the dir where you run the command, what are the subdirs, and in which dir are the tests? Does the test dir have under under init under under .py in it? List some of the files you expect to be run in the test directory. – Heikki Toivonen Oct 22 '09 at 18:46
  • I can't give you guys access to the repo, the tests reside in a 'tests' directory, and it does have an empty init.py. If I import each test file in the init.py and run 'nosetests tests', everything works on ubuntu. But I can't keep updating the init.py whenever I add a new test. I'm wondering why nose behaves differently on Mac and Ubuntu. – Sudhir Jonathan Oct 23 '09 at 08:26

7 Answers7

112

The other thing which always gets me with nose is that it won't run tests in executable files. I'm not exactly sure why that would make a difference across Mac/Ubuntu, but it's worth a shot.

Make sure that the scripts didn't somehow get chmod +x'd on the Mac… And if they did, fix them with chmod -x $(find tests/ -name '*.py').

David Wolever
  • 148,955
  • 89
  • 346
  • 502
65

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).

Peter
  • 628
  • 7
  • 12
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 2
    will try that... the thing is, the same files bet picked up on my Mac... doesn't nose use the same matcher across all platforms? – Sudhir Jonathan Sep 22 '09 at 05:45
  • Your NOSE_TESTMATCH environment variable might be set differently, or take a look at nose/config.py. http://code.google.com/p/python-nose/wiki/FindingAndRunningTests – Mark Rushakoff Sep 22 '09 at 10:47
  • The tests work when I import the test file in the __init__.py and specify the module to run. Not otherwise. The exact same directory and commands work fine on my Mac, though, even without explicitly specifying the directory. Still not able to figure it out. – Sudhir Jonathan Oct 16 '09 at 18:13
  • The first link ('the nose docs') redirects to readthedocs and is nonexistent. – skytreader Apr 15 '14 at 04:52
  • 4
    I was missing the __init__.py file in the test module directory. This fix worked like a charm! – FearlessFuture Aug 07 '14 at 19:49
47

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
rocky
  • 1,037
  • 1
  • 11
  • 19
  • I have some tests which say, that these file will be skipped because they are executables, and then it STILL goes in and uses them. While in the other app of the same project it does really skip them. Took me quite some time to find out that A) tests were missing, B) nosetest and not django is at fault and C) --exe is the solution! – Chris Aug 09 '19 at 15:34
  • This fix worked like a charm!! I've been trying for so long time and your --exe filed it!! – Wesam Na Oct 17 '19 at 10:43
  • I work on Windows though our docker containers run in linux. So I got this problem and fixed it as per @rocky answer. I'm also not sure if cloning this repo in windows is what made all files executable? guess that's a windows related question. – Alvaro Rodriguez Scelza Jan 20 '22 at 20:43
19

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.

sorin
  • 161,544
  • 178
  • 535
  • 806
zahanm
  • 993
  • 8
  • 11
11

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.

MITjanitor
  • 375
  • 1
  • 3
  • 16
2

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

nosetests --all-modules ./tests

georgeok
  • 5,321
  • 2
  • 39
  • 61
1

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.

seaders
  • 3,878
  • 3
  • 40
  • 64