Search code examples
pythonpyramidnose

nose runs test on function in setUp when no suite specified


I have a tests file in one of my Pyramid projects. It has one suite with six tests in it:

...
from .scripts import populate_test_data

class FunctionalTests(unittest.TestCase):

    def setUp(self):
        settings = appconfig('config:testing.ini',
                             'main',
                             relative_to='../..')
        app = main({}, **settings)
        self.testapp = TestApp(app)
        self.config = testing.setUp()
        engine = engine_from_config(settings)
        DBSession.configure(bind=engine)
        populate_test_data(engine)

    def tearDown(self):
        DBSession.remove()
        tearDown()

    def test_index(self):
        ...

    def test_login_form(self):
        ...

    def test_read_recipe(self):
        ...

    def test_tag(self):
        ...

    def test_dish(self):
        ...

    def test_dashboard_forbidden(self):
        ...

Now, when I run nosetests templates.py (where templates.py is the mentioned file) I get the following output:

......E
======================================================================
ERROR: templates.populate_test_data
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/yentsun/env/local/lib/python2.7/site-packages/nose-1.1.2-py2.7.egg/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/yentsun/env/local/lib/python2.7/site-packages/nose-1.1.2-py2.7.egg/nose/util.py", line 622, in newfunc
    return func(*arg, **kw)
TypeError: populate_test_data() takes exactly 1 argument (0 given)

----------------------------------------------------------------------
Ran 7 tests in 1.985s

FAILED (errors=1)

When I run the tests with test suite specified nosetests templates.py:FunctionalTests, the output is, as expected, ok:

......
----------------------------------------------------------------------
Ran 6 tests in 1.980s

OK

Why do I have different output and why is an extra (7th) test run?

UPDATE. Its a bit frustrating, but I when removed the word test from the name populate_test_data (it became populate_dummy_data), everything worked fine.

The problem is solved for now, but maybe somebody knows what went wrong here - why a function from setUp had been tested?


Solution

  • Finding and running tests

    nose, by default, follows a few simple rules for test discovery.

    • If it looks like a test, it’s a test. Names of directories, modules, classes and functions are compared against the testMatch regular expression, and those that match are considered tests. Any class that is a unittest.TestCase subclass is also collected, so long as it is inside of a module that looks like a test.

    (from nose 1.3.0 documentation)

    In the nose's code, the regexp is defined as r'(?:^|[\b_\.%s-])[Tt]est' % os.sep, and if you inpect nose/selector.py, method Selector.matches(self, name) you'll see that the code uses re.search, which looks for a match anywhere in the string, not only at the beginning, as re.match does.

    A small test:

    >>> import re
    >>> import os
    >>> testMatch = r'(?:^|[\b_\.%s-])[Tt]est' % os.sep
    >>> re.match(testMatch, 'populate_test_data')
    >>> re.search(testMatch, 'populate_test_data')
    <_sre.SRE_Match object at 0x7f3512569238>
    

    So populate_test_data indeed "looks like a test" by nose's standards.