def test_correct_page(self): """ make sure a valid html document is returned for index """ request = HttpRequest() response = views.index(request) expected_html = render_to_string('blog/index.html') self.assertEqual(response.content.decode(), expected_html)
def test_index_content(self): request = HttpRequest() response = views.index(request) content = response.content.decode("utf-8") pattern = r'<h1\s+class\s*=\s*\"page-header\">\s*Page\s+Heading\s+<small>\s*Secondary\s+Text\s*</small>\s*</h1>' self.assertFalse(re.search(pattern, content, re.IGNORECASE) is None)
def test_index_content(self): """15) Добавить представление index в файле blog/views.py, которое должно возвращать отрендеренный шаблон blog/index.html""" request = HttpRequest() response = views.index(request) content = response.content.decode("utf-8") pattern = r'<h1\s+class\s*=\s*\"my-4\">\s*Page\s+Heading\s+<small>\s*Secondary\s+Text\s*</small>\s*</h1>' self.assertRegex(content, pattern)
def get_page(self, number=0): """Get the specified page, or first page withoutr parameter""" if number == 0: self.request = self.factory.get('/') else: self.request = self.factory.get('/?page={}'.format(number)) self.response = views.index(self.request) self.soup = BeautifulSoup(self.response.content, 'html.parser') self.pagination = self.soup.find_all('ul', class_='pagination')
def auth_view(request): if request.method == 'POST': #判断是否为一个POST请求 username = request.POST.get("username") #获取表单中用户名称 password = request.POST.get("password") #获取表单中用户密码 user = authenticate(username=username, password=password) #调用authenticate认证用户 if user is not None: if user.is_active: login(request, user) return views.index(request) # Redirect to a success page else: response = HttpResponse() response.write( '<html><script type="text/javascript">alert("密码错误"); window.location=""</script></html>' ) return response # Return an 'invalid login' error message. else: form = LoginForm() return render(request, 'blog/login.html', {'form': form})
def test_index_page_displays_email_in_html(self): request = HttpRequest() response = index(request) html = response.content.decode('utf8') self.assertIn('Dan Chay', html)
def test_index_page_displays_search_form_in_html(self): request = HttpRequest() response = index(request) html = response.content.decode('utf8') self.assertIn('Search in titles and tags', html)
def test_index_static_links(self): request = HttpRequest() response = views.index(request) content = response.content.decode("utf-8") self.if_present_static_links(content)
def test_home_page_returns_correct_html(self): request = HttpRequest() response = index(request) self.assertIn(b'<title>Welcome to my blog</title>', response.content)
def test_index(rf): request = rf.get('127.0.0.1:800/index') response = index(request) assert response.status_code == 200
def test_index(rf): request = rf.get('127.0.0.1:800/blog/details/1/') response = index(request) assert response.status_code == 200