2014-11-14 42 views
0

環境的初始夾具:的Django 1.7:問題加載測試

Django的版本:1.7.1
鼻版本:1.3.4
Django的鼻子版本:1.2

我有兩個應用:
狗和人類

該模型有:

class Dog(models.Model): 
    human = models.ForeignKey('humans.Human', null=False) 

    def askFood(human): 
     ... 

人類

和在模型中有一個函數像在實例屬性的人力,像

name = models.CharField(_('Name'), null=False, max_length=120) 

的兩個應用程序列在INSTALLED_APPS在設置文件中。

的FIXURE_DIRS如下:

FIXTURE_DIRS = (
    os.path.join(os.path.dirname(__file__), '../**/tests'), 
) 

測試是在狗的應用程序的測試文件夾中。

我有一個測試狗應用程序來測試askFood funcion,我有一個JSON夾具(initial_data)與人類。

[ 
    { 
     "pk": 1, 
     "model": "human.Human", 
     "fields": { 
      "name": "Test" 
     } 
    }, 
] 

測試是:

class DogFixtureTestCase(TestCase): 
    fixtures = ['./initial_data.json'] 

class TestFixtureDog(DogFixtureTestCase): 
    def test_ask_food(self): 
     ... 

我執行命令測試:

python manage.py test dog 

有一個錯誤:

no such table: dog_dog 

顯然它不是同步的所有這些應用程序正在創建只有do的表g,當燈具運行時,它找不到人表。我如何強制測試爲所有應用程序創建模型?

在此先感謝您的幫助:)

[編輯]

我改變了標題,並張貼的答案,也許幫助別人同樣的問題。

問題不在於同步不會創建所有表,而是錯誤消息。問題在於django 1.7中不支持的初始數據裝置。

+1

聽起來像遷移問題。我還沒有機會使用Django 1.7遷移,但請確保遵循文檔中的建議,我認爲這將解決您的問題。 https://docs.djangoproject.com/en/1.7/topics/migrations/ – 2014-11-14 17:00:50

回答

0

好吧,我會回答我的問題:)

我發現了一個bug report在Django的bug跟蹤系統:

這個問題不加載夾具,但只是初步數據。

Note that ​initial data isn't supported if an application has migrations. It may be worth throwing a more informative error message though.

因此很明顯,有三個選項:

  • 遷移燈具到新的 「Data Migrations」。
  • 使用setUp單元測試以編程方式加載數據。
  • 不要從fixtures的「路徑」中自動加載inital_data.json(刪除initial_data.json)。問題是加載初始數據而不是燈具。