Django测试客户端只能登录一次?

问题描述:

我们的团队目前正在为我们的应用程序编写测试。我目前正在编写代码来访问视图。这些视图位于登录屏幕后面,因此我们的测试首先必须登录并执行其余的测试。我遇到了一个非常奇怪的错误。基本上我的测试只能登录一次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

我的猜测是,这是因为你自己的实例客户在setUp。虽然看起来很好,但结果显然不同于常规行为。我从来没有使用预先初始化的self.clientdjango.test.TestCase进行登录时遇到问题。

django.test.client.Client,它说,联文档中:

客户对象是有状态的 - 他们将保留cookie的(因而会)详细信息客户实例的生命周期。

和仍然存在的cookie会解释您描述的行为。

我找不到HTTP_HOSTdjango.test.client.py,所以我不确定你是否真的在使用那个客户端类。如果您需要在测试期间访问实时服务器实例,则可以使用Django的LiveServerTestCase