【问题标题】:Getting nose to ignore a function with 'test' in the name让鼻子忽略名称中带有“测试”的功能
【发布时间】:2015-11-07 08:31:07
【问题描述】:

nose 发现过程查找名称以test 开头的所有模块,以及其中所有名称中包含test 的函数,并尝试将它们作为单元测试运行。见http://nose.readthedocs.org/en/latest/man.html

我在文件accounts.py 中有一个名为make_test_account 的函数。我想在名为test_account 的测试模块中测试该功能。所以在该文件的开头我会这样做:

from foo.accounts import make_test_account

但现在我发现nose将函数make_test_account视为单元测试并尝试运行它(失败是因为它没有传入任何必需的参数)。

如何确保鼻子专门忽略该功能?我更愿意以一种方式来执行此操作,这意味着我可以以nosetests 调用nose,而无需任何命令行参数。

【问题讨论】:

    标签: python unit-testing nose


    【解决方案1】:

    告诉鼻子该函数不是测试 - 使用 nottest 装饰器。

    # module foo.accounts
    
    from nose.tools import nottest
    
    @nottest
    def make_test_account():
        ...
    

    【讨论】:

      【解决方案2】:

      鼻子有一个nottest 装饰器。但是,如果您不想在要从中导入的模块中应用 @nottest 装饰器,也可以在导入后简单地修改方法。将单元测试逻辑保持在单元测试本身附近可能会更清晰。

      from foo.accounts import make_test_account
      # prevent nose test from running this imported method
      make_test_account.__test__ = False
      

      您仍然可以使用nottest,但效果相同:

      from nose.tools import nottest
      from foo.accounts import make_test_account
      # prevent nose test from running this imported method
      make_test_account = nottest(make_test_account)
      

      【讨论】:

      • 这看起来比 dm295 接受的答案要简洁得多,因为它不会强制在生产中添加特定于测试(更重要的是 - 特定于测试框架)的代码代码。感谢您的回答!
      • 这似乎更好,因为它可以跨测试运行器工作,而不会引入鼻子依赖。
      猜你喜欢
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多