2017-08-31 35 views
0

我們的團隊目前正在爲我們的應用程序編寫測試。我目前正在編寫代碼來訪問視圖。這些視圖位於登錄屏幕後面,因此我們的測試首先必須登錄並執行其餘的測試。我遇到了一個非常奇怪的錯誤。基本上我的測試只能登錄一次Django測試客戶端只能登錄一次?

正如你在下面的例子中看到的,兩個類都做着完全相同的事情,但其中只有一個成功登錄,另一個給出'302 doest not equal 200'斷言錯誤。

如果我註釋掉底部的那個,頂部的那個會工作,反之亦然。 測試不同視圖的代碼也不起作用,除非我註釋掉全部其他測試。

它不重要,如果我登錄像下面所示,或使用不同的變種(如self.client.login(username ='test',password ='password'))。

我和我的團隊不知道爲什麼Django會這樣做,我們做錯了什麼。它幾乎就好像連接保持打開狀態,我們將不得不添加代碼來關閉它。但是django文檔並沒有提到這一點。有誰知道我們做錯了什麼?

class FunctieListView_tests(TestCase): 
"""Function listview only shows the data for the current_user/tenant""" 

def setUp(self): 
    self.tenant = get_tenant() 
    self.function = get_function(self.tenant) 
    self.client = Client(HTTP_HOST='tc.tc:8000') 
    self.user = get_user(self.tenant) 


def test_correct_function_context(self): 
    # Test if the view is only displaying the correct context data 
    self.client.post(settings.LOGIN_URL, { 
     'username': self.user.username, 
     'password': 'password' 
    }, HTTP_HOST='tc.tc:8000') 
    response = self.client.get(reverse('functie_list')) 
    self.assertEqual(response.status_code, 200) 
    self.assertTrue(response.context['functie_templates'] != None) 
    self.assertEqual(response.context['functie_templates'][0], 
        FunctieTemplate.objects.filter(linked_tenant=self.tenant)[0]) 


class FunctieListView_2_tests(TestCase): 
"""Role Listview only shows the data for the current_user/tenant""" 

def setUp(self): 
    self.tenant = get_tenant() 
    self.function = get_function(self.tenant) 
    self.client = Client(HTTP_HOST='tc.tc:8000') 
    self.user = get_user(self.tenant) 

def test_correct_function_context_second(self): 
    #login 
    # Test if the view is only displaying the correct context data 
    self.client.post(settings.LOGIN_URL, { 
     'username': self.user.username, 
     'password': 'password' 
    }, HTTP_HOST='tc.tc:8000') 
    response = self.client.get(reverse('functie_list')) 
    self.assertEqual(response.status_code, 200) 
    self.assertTrue(response.context['functie_templates'] != None) 
    self.assertEqual(response.context['functie_templates'][0], 
        FunctieTemplate.objects.filter(linked_tenant=self.tenant)[0]) 

的用戶,租戶和功能都在一個單獨的文件utils的,像這樣定義的:

def get_user(tenant, name='test'): 

    u = User.objects.create_user(name, '{}@test.test'.format(name), 'password') 
    u.save() 
    u.profile.tenant = tenant 
    u.profile.tenant_role = generis.models.TENANT_OWNER 
    u.profile.save() 
    return u 


def get_function(tenant): 

    userfunction = UserFunction.objects.create(name='test_functie', linked_tenant=tenant) 
    userfunction.save() 
    return userfunction 


def get_tenant(slug_var='tc'): 
    f = elearning.models.FontStyle(font='foobar') 
    f.save() 
    c = elearning.models.ColorScheme(name='foobar', title='foo', text='fleeb', background='juice', block_background='schleem', box='plumbus') 
    c.save() 
    t = elearning.models.Tenant(name='tc', slug=slug_var, default_font_style=f, default_color_scheme=c) 
    t.save() 
    return t 
+0

你可以爲'TestCase'類添加import語句嗎? – Risadinha

回答

1

我的猜測是,這是因爲你自己的實例客戶在setUp。雖然看起來很好,但結果顯然不同於常規行爲。我從來沒有使用預先初始化的self.clientdjango.test.TestCase進行登錄時遇到問題。

django.test.client.Client,它說,聯文檔中:

客戶對象是有狀態的 - 他們將保留cookie的(因而會)詳細信息客戶實例的生命週期。

和仍然存在的cookie會解釋您描述的行爲。

我找不到HTTP_HOSTdjango.test.client.py,所以我不確定你是否真的在使用那個客戶端類。如果您需要在測試期間訪問實時服務器實例,則可以使用Django的LiveServerTestCase