python - 获取类型错误(): test_if_module_exists() when running nosetests

标签 python nose nosetests

更新:如果我改变

from scitools.std import *

例如

from scitools.std import sqrt, zeros

一切正常..

我正在尝试运行 nonestests -s myfile.py,但我总是收到此错误:

======================================================================
ERROR: Test if modulename can be imported, and if not, write
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/usr/local/lib/python2.7/dist-packages/nose/util.py", line 613, in newfunc
    return func(*arg, **kw)
TypeError: test_if_module_exists() takes at least 1 argument (0 given)

程序:

from scitools.std import *
import nose.tools as nt

def test_test():

    diff = 1E-6
    nt.assert_almost_equal(diff, 0, delta=1E-5)

def main():
    print __name__

if __name__ == "__main__":
    main()

我正在运行 Nose 1.3.0。网上找了很多解决办法,没找到!

谢谢大家!

最佳答案

因为您正在使用通配符导入...不要使用通配符!

基本上当你说

from scitools.stf import *

您还从以下位置导入所有内容:

  • scitools.easyviz
  • scitools.basics

nose 的工作方式是在模块中查找所有名为 test_ 的函数。这包括您编写的每个函数和您导入的每个函数。这称为Duck typing 。因此,如果您不希望 Nose 尝试运行某个函数,请不要导入它。当您使用通配符导入时,您正在导入所有内容,这就是为什么通配符不是一个好主意的原因。只需导入您需要的功能即可,例如

from scitools.std import sqrt, zeros

关于python - 获取类型错误(): test_if_module_exists() when running nosetests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18375542/

相关文章:

python - 使用 init_model 中的模型对 Pylons 应用程序进行 Nose 测试?

python - notest - 使用 sys.tracebacklimit = 0 抑制回溯不会生效

python - 有没有办法让 python 的 nose 模块在 __main__ 和命令行中同样工作?

Python 导入模块 : how to track its orgin PYTHONPATH, sys, os

python - 覆盖范围未显示 virtualenv 中执行的行

python - Scrapy嵌套页面爬取

python - 如何改进在 Python 中移动所有不包含特定日期的文件?

python - 无法在Python中从statsforecast.models导入Adida

python - 为 ndb.tasklets 类型注释

python - 通过 Nose 测试与多个实体组运行事务时出错