当没有指定套件时,nose运行setUp中的函数测试

时间:2013-05-10 05:49:06

标签: python pyramid nose

我在我的一个Pyramid项目中有一个测试文件。它有一个套件,其中包含六个测试:

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

现在,当我运行nosetests templates.py(其中templates.py是提到的文件)时,我得到以下输出:

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

当我使用指定的nosetests templates.py:FunctionalTests测试套件运行测试时,输出正如预期的那样好:

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

OK

为什么我有不同的输出?为什么要进行额外的(第7次)测试?

更新。它有点令人沮丧,但我从名称populate_test_data(它成为populate_dummy_data)中删除了 test 这个词,一切工作得很好。

问题现在已经解决了,但也许有人知道这里出了什么问题 - 为什么来自setUp的函数已经过测试?

1 个答案:

答案 0 :(得分:3)

  

查找并运行测试

     默认情况下,

nose遵循一些简单的测试发现规则。

     
      
  • 如果它看起来像一个测试,那就是一个测试。将目录,模块,类和函数的名称与 testMatch regular进行比较   表达式,那些匹配的被认为是测试。任何课程   是一个unittest.TestCase子类也被收集,只要它是   在看起来像测试的模块内部。
  •   

(来自nose 1.3.0 documentation

在鼻子的代码中,正则表达式定义为r'(?:^|[\b_\.%s-])[Tt]est' % os.sep,如果您查看nose/selector.py方法Selector.matches(self, name),您会看到代码使用re.search,在字符串中的任何位置查找匹配项,而不仅仅是re.match所在的开头。

小测试:

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

所以populate_test_data确实“看起来像鼻子的标准”。