nose treats methods as tests even if I supply a tests directory path on the command line

32 views
Skip to first unread message

Monty Hindman

unread,
Mar 22, 2015, 5:39:29 PM3/22/15
to nose...@googlegroups.com
I work on a project where many of our classes and functions include "test" in their names. As a result, nose confuses them for tests. Here is a fake example:

./
  justice/
    __init__.py
    testimony.py
  tests/
    __init__.py
    testimony_tests.py

I had assumed that simply passing my tests directory on the command line would fix this problem:

$ nosetests --verbosity 2 tests/
NO: this is not a test ... ok
YES: test_bar ... ok
YES: test_foo ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s

But when testimony_tests.py imports testimony.py, nose treats methods in that class as potential tests.

Is this intended behavior? It seems counter-intuitive to me (but I haven't thought about it very hard).

I do know about three workarounds:

-- Use the @nottest decorator throughout our project (not convenient for our project and team).
-- Supply full paths, with module names, on the command line (even less convenient).
-- Write our our plugin (fun, but hoping for something less involved).

Aside from those options, is there a simple way to solve this problem?

Thank you.

Example code follows:

################################

# testimony.py
class Testimony(object):
    def blah_test_blah(self):
        'NO: this is not a test'
        pass

# testimony_tests.py
import unittest
from justice.testimony import Testimony

class Testimony_TC(unittest.TestCase):
    def test_bar(self):
        'YES: test_bar'
        pass
    def test_foo(self):
        'YES: test_foo'
        pass

Björn Pedersen

unread,
Jul 5, 2016, 7:07:59 AM7/5/16
to nose-dev
Hi,

you need to tune your nose settings:

https://nose.readthedocs.io/en/latest/finding_tests.html explains the logic of finding tests. So setting --testmatch to a suitable regex should help.
For conveniece, I would put set into a nose.cfg file in the project.

From:
https://nose.readthedocs.io/en/latest/usage.html#extended-usage

-m=REGEX, --match=REGEX, --testmatch=REGEX

Files, directories, function names, and class names that match this regular expression are considered tests. Default: (?:\b|_)[Tt]est [NOSE_TESTMATCH]


Björn
Reply all
Reply to author
Forward
0 new messages