2016-03-07 111 views
0

我想爲我的tastypie應用程序運行一些測試,但我得到以下錯誤。單元測試在Django/Tastypie

Finding files... done. 
Importing test modules ... Traceback (most recent call last): 
    File "C:\Users\user\.p2\pool\plugins\org.python.pydev_4.4.0.201510052309\pysrc\pydev_runfiles.py", line 468, in __get_module_from_str 
    mod = __import__(modname) 
    File "DjangoTastypie/myApp\tests.py", line 2, in <module> 
    from tastypie.test import ResourceTestCaseMixin 
    File "C:\Python27\lib\site-packages\tastypie\test.py", line 9, in <module> 
    from tastypie.serializers import Serializer 
    File "C:\Python27\lib\site-packages\tastypie\serializers.py", line 12, in <module> 
    from tastypie.exceptions import BadRequest, UnsupportedFormat 
    File "C:\Python27\lib\site-packages\tastypie\exceptions.py", line 83, in <module> 
    class ImmediateHttpResponse(TastypieError): 
    File "C:\Python27\lib\site-packages\tastypie\exceptions.py", line 94, in ImmediateHttpResponse 
    _response = HttpResponse("Nothing provided.") 
    File "C:\Python27\lib\site-packages\django\http\response.py", line 340, in __init__ 
    super(HttpResponse, self).__init__(*args, **kwargs) 
    File "C:\Python27\lib\site-packages\django\http\response.py", line 125, in __init__ 
    content_type = '%s; charset=%s' % (settings.DEFAULT_CONTENT_TYPE, 
    File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__ 
    self._setup(name) 
    File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 44, in _setup 
    self._wrapped = Settings(settings_module) 
    File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 92, in __init__ 
    mod = importlib.import_module(self.SETTINGS_MODULE) 
    File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module 
    __import__(name) 
ImportError: No module named DjangoTastypie.settings 
ERROR: Module: tests could not be imported (file: DjangoTastypie\myApp\tests.py). 
done. 
Traceback (most recent call last): 
    File "C:\Users\user\.p2\pool\plugins\org.python.pydev_4.4.0.201510052309\pysrc\runfiles.py", line 234, in <module> 
    main() 
    File "C:\Users\use\.p2\pool\plugins\org.python.pydev_4.4.0.201510052309\pysrc\runfiles.py", line 78, in main 
    return pydev_runfiles.main(configuration) # Note: still doesn't return a proper value. 
    File "C:\Users\user\.p2\pool\plugins\org.python.pydev_4.4.0.201510052309\pysrc\pydev_runfiles.py", line 835, in main 
    PydevTestRunner(configuration).run_tests() 
    File "C:\Users\user\.p2\pool\plugins\org.python.pydev_4.4.0.201510052309\pysrc\pydev_runfiles.py", line 793, in run_tests 
    MyDjangoTestSuiteRunner(run_tests).run_tests([]) 
    File "C:\Users\user\.p2\pool\plugins\org.python.pydev_4.4.0.201510052309\pysrc\pydev_runfiles.py", line 813, in run_tests 
    raise AssertionError("Unable to run suite with DjangoTestSuiteRunner because it couldn't be imported.") 
AssertionError: Unable to run suite with DjangoTestSuiteRunner because it couldn't be imported. 

是否需要將某些東西添加到settings.py的installed_apps部分?或者我的測試需要位於其他地方?現在我正在使用當你啓動一個新的Django項目時獲得的基本結構,並且我的測試是在tests.py

好吧,我發現問題出在Eclipse - > Run as pyunit選項。現在當我運行它使用manage.py測試我得到這個錯誤

====================================================================== 
ERROR: myApp.tests (unittest.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
ImportError: Failed to import test module: myApp.tests 
Traceback (most recent call last): 
    File "C:\Python27\Lib\unittest\loader.py", line 254, in _find_tests 
    module = self._get_module_from_name(name) 
    File "C:\Python27\Lib\unittest\loader.py", line 232, in _get_module_from_name 
    __import__(name) 
    File "C:\Users\dhanushka.amarakoon\git\lcapi\DjangoTastypie\myApp\tests.py", line 2, in <module> 
    from tastypie.test import ResourceTestCaseMixin 
ImportError: cannot import name ResourceTestCaseMixin 
+0

你能提供'tests.py'文件嗎? – silviomoreto

+0

哪個版本的Django?你使用什麼命令來運行你的測試?另外,請向我們顯示您的manage.py文件。 –

+0

我認爲這個錯誤是由於我運行測試使用日食run作爲pyunit選項。 –

回答

0

好吧,我想出了我做錯了什麼。首先,我使用的是eclipse-> run和pyunit,它使用的是舊代碼。然後我得到了導入錯誤因爲我使用了錯誤的類名ResourceTestCaseMixin而不是ResourceTestCase