6
項目

當我嘗試在我的Django(1.4)項目運行python manage.py test,我得到的錯誤:測試僅在Django

ERROR: test_site_profile_not_available (django.contrib.auth.tests.models.ProfileTestCase) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/home/slacy/src/tmp/env/local/lib/python2.7/site-packages/django/contrib/auth/tests/models.py", line 29, in test_site_profile_not_available 
    del settings.AUTH_PROFILE_MODULE 
    File "/home/slacy/src/tmp/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 215, in __delattr__ 
    delattr(self._wrapped, name) 
AttributeError: AUTH_PROFILE_MODULE 

這是一個Django的bug documented,具有recomendation只測試特定的應用程序而不是全部。但是我的項目沒有應用程序,models.py只是駐留在項目根目錄中。要在Django測試特定應用,它看起來像this

$ ./manage.py test animals 
Note that we used animals, not myproject.animals. 

這意味着它是不可能指定的根目錄來進行測試。我將如何測試我的項目目錄?

請注意,該錯誤是larger discussion on unit testing discovery的一部分。

回答

3

我會推薦使用django-discover-runner。它允許你指定和完整的虛線路徑來測試。

If you just run ./manage.py test, it'll discover and run all tests underneath the TEST_DISCOVER_ROOT setting (a file system path). If you run ./manage.py test full.dotted.path.to.test_module, it'll run the tests in that module (you can also pass multiple modules). If you give it a single dotted path to a package (like a Django app) like ./manage.py test myapp and that package does not itself directly contain any tests, it'll do test discovery in all submodules of that package.

+2

從Django 1.6開始,有一個[new test discover runner](https://docs.djangoproject.com/en/dev/releases/1.6/#discovery-of-tests-in-any-test-module),它確切地說,django-discover-runner在1.5和更低版本中的表現。 – alfetopito

0

創建一個名爲應用新的目錄,將你的整個應用到它減去settings.py,manage.py,urls.py和任何其他默認添加由django-admin.py startproject命令,然後運行

./manage.py test apps 

將來,避免將整個應用程序保留在一個目錄中。將其分爲基於功能的子應用程序,基於這個原因

+0

但是我的項目只有四個模型,它們都與我相關。我認爲只有沒有應用程序的項目沒有錯,只要它不會太大。 –

+0

直到另一個解決方案出現時,我想我只會製作一個應用程序並將所有內容都放在那裏。 –

+0

我分離了我的項目,並意識到實際上它很有幫助。 –