【发布时间】: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 test 或python manage.py test myproject.core.tests,那么所有测试(包括上述文件中的测试)都运行良好。我找到了this 解决方案,但我的测试文件似乎可以正常导入,没有任何错误或警告。还有什么我可能会遗漏的吗?
【问题讨论】:
-
文件 myproject/core/tests/__init__.py 是否存在?
-
它确实存在(就像一个空文件)。此外,该目录中的其他文件在使用测试运行程序调用时也可以工作。
-
您是否尝试清理所有 .pyc 文件(查找 .-name ".pyc" -exec rm -rf {} \;)?
-
感谢您的快速回复!我刚刚尝试过,仍然得到相同的结果。
-
请添加您的测试文件结构和相关测试
标签: python django unit-testing