2017-08-31 5 views
0

私たちのチームは現在、アプリケーションのテストを行っています。私は現在、ビューにアクセスするコードを書いています。これらのビューはログイン画面の背後にあるので、最初にログインし、残りのテストを実行する必要があります。私は非常に奇妙なエラーに遭遇しました。基本的に私のテストはに一度しかログインできません。Djangoテストクライアントは一度だけログインできますか?

下の例から分かるように、両方のクラスはまったく同じことをしていますが、一方はログインで成功し、もう一方は「302 doest not equal 200」アサーションエラーが発生します。

私が下のものをコメントアウトすると、上のものがコメントアウトされ、その逆も同様です。 私がコメントアウトしない限り、別のビューをテストするコードもありませんすべて他のテスト。

以下に示すようにログインするか、別の亜種(self.client.login(username = 'test'、password = 'password'など)を使用しても問題ありません)。

私と私のチームは、Djangoがこのように動作していること、そして私たちが間違っていることを知りません。接続が開いたままであるかのように、それを閉じるコードを追加する必要があります。しかし、django-documentationにはこれが言及されていません。私たちが間違っていることを誰かが知っていますか?

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を見ると、それはインラインドキュメントに書かれています:

Clientオブジェクトは、ステートフルです - 彼らは、クライアント・インスタンスの存続のためにクッキー(したがってセッション)の詳細を保持します。

あなたが説明している動作を説明します。

django.test.client.pyHTTP_HOSTが見つかりません。実際にそのクライアントクラスを使用しているかどうかはわかりません。テスト中にライブサーバーインスタンスにアクセスする必要がある場合は、DjangoのLiveServerTestCaseを使用できます。

関連する問題