API测试用例 - 'module'对象没有属性

时间:2015-03-30 07:40:16

标签: django python-3.x django-rest-framework

$ python manage.py test homepage.shortlistedlist

当我运行django rest framework testcase文件时,我遇到了以下错误,请帮我解决这个问题,

(py3.4)testuser@testuser-To:~/projects/testfile/testfile$ python manage.py test homepage.compareproperties --settings=testfile.settings.test
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/testuser/envs/testfile/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/testuser/envs/testfile/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/testuser/envs/testfile/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 "/home/testuser/envs/testfile/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/testuser/envs/testfile/lib/python3.4/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/home/testuser/envs/testfile/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/testuser/envs/testfile/lib/python3.4/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/testuser/envs/testfile/lib/python3.4/site-packages/django/test/runner.py", line 146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/home/testuser/envs/testfile/lib/python3.4/site-packages/django/test/runner.py", line 95, in build_suite
    tests = self.test_loader.discover(start_dir=label, **kwargs)
  File "/usr/lib/python3.4/unittest/loader.py", line 255, in discover
    self._get_directory_containing_module(top_part)
  File "/usr/lib/python3.4/unittest/loader.py", line 269, in _get_directory_containing_module
    full_path = os.path.abspath(module.__file__)
AttributeError: 'module' object has no attribute '__file__'

先谢谢,

1 个答案:

答案 0 :(得分:2)

我能够通过在项目的根目录中添加一个空的__init__.py来解决这个问题。

我认为线索是get_directory_containing_module()调用 - 没有__init__.py,它不会将目录视为模块。