【问题标题】:Suddenly when running tests I get "TypeError: 'NoneType' object is not iterable运行测试时突然出现“TypeError:'NoneType'对象不可迭代
【发布时间】:2015-07-03 12:10:18
【问题描述】:

这对我来说非常奇怪,直到今天早上一切都很好。

当我尝试使用以下内容运行我的单元测试时(我将 Python3 软链接到 python)

clear; python manage.py test list tests/  

我现在收到以下错误消息:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python3.4/site-packages/django/core/management        /__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3.4/site-packages/django/core/management        /__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3.4/site-packages/django/core/management/commands    /test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/usr/lib/python3.4/site-packages/django/core/management    /base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/python3.4/site-packages/django/core/management/commands    /test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/usr/lib/python3.4/site-packages/django/core/management    /base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python3.4/site-packages/django/core/management/commands    /test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/usr/lib/python3.4/site-packages/django/test/runner.py", line     146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/usr/lib/python3.4/site-packages/django/test/runner.py", line     101, in build_suite
    suite.addTests(tests)
  File "/usr/lib64/python3.4/unittest/suite.py", line 60, in addTests
    for test in tests:
TypeError: 'NoneType' object is not iterable

我最初认为我写的东西完全搞砸了一切,所以我把它保存为一个分支,在 master 分支中恢复到我确定工作的提交,但我得到了完全相同的错误消息。

我想不出我做了什么让事情像这样失败,事实上,上面并没有指出任何我已经写过的东西(在我的情况下,我是对 Python/Django 来说相对较新)让我很难调试错误。

我能想到的唯一改变的是我安装的The Silver Searcher(我使用vim),我删除了它,但仍然发生同样的错误。

我已经重新安装了 django,但仍然无济于事。

这是在:

  • OpenSuse 13.2
  • Django 1.7
  • Python 3.4.1

谁能帮帮我

  • 调试这个错误的原因(以免我再次陷入其中)
  • 如何解决这个问题?

谢谢

【问题讨论】:

  • 您可能有多个包含测试的文件。此错误发生在哪个套件上?
  • 您是指包含测试的 py 文件还是测试套件,如nose 和 tox?
  • 嗯......我发现了一些非常奇怪的东西。如果我运行“python manage.py test list/”这行得通,但是“python manage.py test list/tests.py”它会脱口而出我之前粘贴的错误消息......为什么?
  • 我认为您应该使用点分格式。试试python manage.py test list.tests
  • 查看文档。 docs.djangoproject.com/en/1.8/topics/testing/overview/… 您运行测试的方式无效。点分格式有效。

标签: python django python-3.x django-1.7


【解决方案1】:

刚刚遇到这个错误,由我几个月前解决的一个问题引起,但又不小心再次发生。

问题在于,当您为 django test 指定目录时,您应该对目录使用 python 表示法,而不是标准的 shell 表示法。例如

正确 - project.tests.test_module

不正确 - project/tests/test_module.py

【讨论】:

    【解决方案2】:

    我无法解释为什么会这样,但是当我在没有指定文件名的情况下运行“python manage.py test list/”时,一切正常。

    感谢用户 Ella Shar,我将更改方案/布局,我将我的测试设置为documentation 中所述的经批准的点格式

    【讨论】:

    • 升级 Django 和 PyCharm 后我遇到了同样的问题。使用测试目标的路径曾经可以工作,但升级后出现您指定的确切错误...我将其更改为使用点表示法,它又开始工作了。
    【解决方案3】:

    对我来说,这是一个数据库问题。

    显然,运行在中间停止,他没有时间删除测试的数据库。

    删除数据库测试即可解决问题

    【讨论】:

      猜你喜欢
      • 2012-08-25
      • 1970-01-01
      • 2017-08-29
      • 2016-08-30
      • 2014-11-21
      • 1970-01-01
      相关资源
      最近更新 更多