2015-10-20 117 views
1

我試圖從官方文檔Django測試夾具,但我的測試類找不到assertContains。Django AttributeError:對象沒有屬性'assertContains'

from django.utils import unittest 
from django.test import Client 

class SimpleTest(unittest.TestCase): 
    def setUp(self): 
     self.client = Client() 

    def test_details(self): 
     response = self.client.post('/register', 
            {'username': '123', 
            'password': '123', 
            follow=True) 
     self.assertEqual(response.status_code, 200) 

     self.assertContains(response, "Logout") 
     self.assertNotContains(response, "Login") 

回答

3

assertContains是一個Django特有的功能,不是Python專用的。因此,確保測試類是從django.test中的TestCase子類化的,而不是(python)unittest中的TestCase子類。

from django.test import TestCase 

    class SimpleTest(TestCase): 
     self.assertContains(response, "Logout") 
相關問題