2011-04-04 123 views
1


我在tests.py中有以下內容。django網絡應用程序測試

def setUp(self): 
    self.client = Client() 
    self.client.get('/homepage',{'join':'NPO2','siteid':1450}) 
    self.client.session.save() 
    self.oraganisation_list = ['NPO1','NPO2','NPO3'] 
    self.pay_recursion_list = ['annual','monthly','bi-annual','quarter'] 
def test_paytermpage(self): 
    for org in self.organisation_list: 
     response = self.client.get('',{'join':org,'siteid':1450}) 
     self.failUnlessEqual(response.status_code,200) 
     self.assertTemplateUsed(response,'some.html') 
def test_infopage(self): 
    for term in self.pay_recurstion_list: 
     response = self.client.post('',{'pay-term':term,'submit':'payterm'}) 
     self.failUnlessEqual(response.status_code,200) 

test_infopage()失敗,這裏是回溯。

Traceback (most recent call last): 
    File "/var/lib/django/bsdata/shoppingcart/tests.py", line 50, in test_infopage 
    response = self.client.post('',{'pay-term':term,'submit':'payterm'}) 
    File "/usr/lib/pymodules/python2.6/django/test/client.py", line 313, in post 
    response = self.request(**r) 
    File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 92, in get_response 
    response = callback(request, *callback_args, **callback_kwargs) 
    File "/var/lib/django/.../views.py", line 22, in start 
    term,costdict,webobj = costInfo(request) 
    File "/var/lib/django/...views.py", line 238, in getCostInfo 
    cost_dict = Site.objects.getDict(request.session['siteid']) 
    File "/var/lib/django/.../managers.py", line 16, in getLoadedDict 
    siteobj = Site.objects.get(pk=agent) 
    File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 120, in get 
    return self.get_query_set().get(*args, **kwargs) 
    File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 305, in get 
    % self.model._meta.object_name) 
DoesNotExist: Site matching query does not exist. 

我做了調試,看看有什麼價值「代理人」在siteobj = Site.objects.get(PK =劑)是獲取其有效的整數。

令人驚訝他們兩人正在從殼這樣

setup_test_environment() 
client = Client() 
client.get('/shoppingcart',{'join':'NPO1','siteid':1450}) 
client.session.save() 
oraganisation_list = ['NPO1','NPO2','NPO3'] 
pay_recursion_list = ['annual','monthly','bi-annual','quarter'] 
for org in oraganisation_list: 
    response = client.get('',{'join':org,'siteid':1450}) 
    TestCase.failUnlessEqual(t,response.status_code,200) 
for term in pay_recursion_list: 
    response = client.post('',{'pay-term':term,'submit':'payterm'}) 
    TestCase.failUnlessEqual(t,response.status_code,200) 

對不起太多信息測試時,根本不知道該如何解釋好。 任何想法對這個新手都非常有幫助。謝謝。

回答

2

Django測試運行器使用與生產數據不同的數據庫。如果您的網站需要網站存在,您應該將其添加到您的測試設置中,或者您需要一個加載網站的夾具。

+0

測試代碼中的網站是指具有代理網站詳細信息的表格。我想知道爲什麼它從shell中工作時不給我錯誤:( – stackover 2011-04-05 13:56:13

+0

因爲shell是從你的普通DB工作的。在編寫測試時,你應該提供所有必要的數據在setUp中運行你的測試 – 2011-04-05 15:13:25

+0

Ahhhhhh ....感謝Issac Kelly,在運行測試的同時,我看到了像創建testdatabase和所有modelnames的東西。所以我認爲它使用那些(shell使用的)。我將挖掘文檔來了解如何設置測試數據。但是我非常感謝你的幫助 – stackover 2011-04-05 15:44:27

0

嘗試在設置中放入「SITE_ID = 1」。這是the sites framework的一部分。

+0

感謝您的時間,我確實檢查了我的設置文件中有site_id = 1,可能出錯的任何其他地方? – stackover 2011-04-04 22:51:55