21

We use nose to discover tests and run them. All the tests are written in TestCase compatible way so any test runner can run the. Problem is we have some directories which doesn't have any test. But test runner continue to discover test from there. If one of those directory has lot of files its stuck. So how can I exclude that directory?

Currently I am executing

nosetests --processes=10 --verbosity 2

But there is a directory called scripts which takes ages to discover tests from it. So I want to exclude it. I tried

nosetests --processes=10 --verbosity 2 --exclude='^scripts$'

But no luck.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187

3 Answers3

20

There is a nose-exclude plugin specifically for the task:

nose-exclude is a Nose plugin that allows you to easily specify directories to be excluded from testing.

Among other features, it introduces a new command-line argument called exclude-dir:

nosetests --processes=10 --verbosity 2 --exclude-dir=/path/to/scripts

Instead of passing a command-line argument, you can also set NOSE_EXCLUDE_DIRS environment variable, or set exclude-dir configuration key in .noserc or nose.cfg files.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 2
    So there is not direct way to do these, hence the plugin? – Shiplu Mokaddim Jun 17 '14 at 12:28
  • 1
    @shiplu.mokadd.im I'm afraid plugin is the best option here. `--exclude` nose argument is not applied to directories. I've personally used the plugin and it did the job for me. – alecxe Jun 17 '14 at 12:30
  • @shiplu.mokadd.im one more thing that I will include into the answer if it works for you. Add `__test__ = False` to the `__init__` of `scripts` package. (if `scripts` is a package, of course). Let me know if it works for you or not. Thanks. – alecxe Jun 17 '14 at 13:02
  • For **Django** I had to do `--exclude-dir=/path/to/project/app/tests` - This is the actual *directory* of the tests that I wanted to exclude. Not a parent directory above it. – Aaron Lelevier Dec 15 '15 at 22:13
11

You can also use the --ignore-files argument to exclude specific files. Since this is using regular expressions, you can name your files inside your scripts/ folder to begin with a specific prefix that you can then use to match in a regex.

# Exclude just one test file
nosetests your_package --ignore-files="tests_to_exclude\.py" -v
# Exclude all python scripts beginning with `testskip_`
nosetests your_package --ignore-files="testskip_.+\.py" -v

See: Nose documentation.

gaborous
  • 15,832
  • 10
  • 83
  • 102
  • 1
    `nosetests -s -v -x * --ignore-files="Test1\.py" --ignore-files="Test2\.py"` This is what am trying, But not worked. – Rajat jain Apr 09 '19 at 05:24
4

Perhaps not what the OP asked for, but I found this tidbit from the nose docs useful to exclude a file from consideration:

If an object defines a __test__ attribute that does not evaluate to True, that object will not be collected, nor will any objects it contains.

Kaos
  • 984
  • 7
  • 17
  • This seems to be the only acceptable answer now with `nose2`; sadly this doesn't work when running under python 2 and you're trying to get it to skip a python 3 source file that it can't even parse/compile to get that far... – Aaron D. Marasco May 14 '21 at 11:12