【问题标题】:Django can't find specific test, but runs it if module is definedDjango 找不到特定的测试,但如果定义了模块就运行它
【发布时间】:2025-12-07 18:35:01
【问题描述】:

我在myproject/core/tests/test_views_front.py 进行了一些测试。当我尝试运行 python manage.py test myproject.core.tests.test_views_front 时,我得到:

NOTE: Using test database settings!
NOTE: Logging is disabled!
Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__in
it__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__in
it__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/comm
ands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/base
.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/comm
ands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/base
.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/south/management/commands/t
est.py", line 8, in handle
    super(Command, self).handle(*args, **kwargs)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/comm
ands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/test/runner.py", lin
e 144, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/test/runner.py", lin
e 63, in build_suite
    tests = self.test_loader.loadTestsFromName(label)
  File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'test_views_front'

如果我运行python manage.py test myproject.core.tests.test_views_front.FrontPageViewTests,也会发生这种情况。但是,如果我只是运行python manage.py testpython manage.py test myproject.core.tests,那么所有测试(包括上述文件中的测试)都运行良好。我找到了this 解决方案,但我的测试文件似乎可以正常导入,没有任何错误或警告。还有什么我可能会遗漏的吗?

【问题讨论】:

  • 文件 myproject/core/tests/__init__.py 是否存在?
  • 它确实存在(就像一个空文件)。此外,该目录中的其他文件在使用测试运行程序调用时也可以工作。
  • 您是否尝试清理所有 .pyc 文件(查找 .-name ".pyc" -exec rm -rf {} \;)?
  • 感谢您的快速回复!我刚刚尝试过,仍然得到相同的结果。
  • 请添加您的测试文件结构和相关测试

标签: python django unit-testing


【解决方案1】:

您不能从 Django 中的特定文件运行测试。您可以像以前一样从myproject.core.tests 运行所有测试,也可以从特定的TestCase 运行所有测试:

myproject.core.tests.YourTestCase

【讨论】:

  • 啊,我应该澄清一下运行特定的测试用例会导致相同的错误(我已经更新了问题以反映这一点)。另外,我可以为其他特定文件运行所有测试,所以我认为这不是问题。
  • 那是因为您仍然包含文件名。请注意,Django 将您的所有测试视为一个对象,因此您的文件结构无关紧要。忽略文件名即可。
  • 命令python manage.py test myproject.core.tests.test_views_back 仍然有效,所以这个问题似乎是test_views_front 特有的。