не понимаю что поправить что бы пройти тесты, Python
есть тест:
from http import HTTPStatus
from django.test import Client, TestCase
class StaticPagesURLTests(TestCase):
def setUp(self):
self.guest_client = Client()
def test_about_url_exists_at_desired_location(self):
"""Проверка доступности страниц."""
response = self.guest_client.get('/')
self.assertEqual(response.status_code, HTTPStatus.OK)
response = self.guest_client.get('/second_page/')
self.assertEqual(response.status_code, HTTPStatus.OK)
def test_page_shows_correct_content(self):
"""Проверка контента страниц."""
response = self.guest_client.get('/')
self.assertContains(response, 'У меня получилось!')
response = self.guest_client.get('/second_page/')
self.assertContains(response, 'А это вторая страница!')
Есть вьюха и урл
from django.http import HttpResponse
def index(request):
data = 'У меня получилось!'
return HttpResponse(data, status_code=200)
def second_page(request):
return HttpResponse('А это вторая страница', status_code=200)
url
from django.urls import path
from . import views
app_name = 'infra_app'
urlpatterns = [
path('', views.index, name='index'),
path('infra_app/second/', views.second_page, name='second_page'),
]
не понимаю что нужно поменять что бы пройти тест, пытался в ручную вернуть status_code=200 или HTTPStatus.OK (конечно делал from http import HTTPStatus) - всеравно тесты не проходит
Ошибка по тесту:
FAIL: test_about_url_exists_at_desired_location (infra_app.tests.StaticPagesURLTests)
Проверка доступности страниц.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/work/infra_actions/infra_actions/infra_project/infra_app/tests.py", line 16, in test_about_url_exists_at_desired_location
self.assertEqual(response.status_code, HTTPStatus.OK)
AssertionError: 404 != <HTTPStatus.OK: 200>
======================================================================
FAIL: test_page_shows_correct_content (infra_app.tests.StaticPagesURLTests)
Проверка контента страниц.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/work/infra_actions/infra_actions/infra_project/infra_app/tests.py", line 24, in test_page_shows_correct_content
self.assertContains(response, 'А это вторая страница!')
File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/test/testcases.py", line 446, in assertContains
response, text, status_code, msg_prefix, html)
File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/test/testcases.py", line 418, in _assert_contains
" (expected %d)" % (response.status_code, status_code)
AssertionError: 404 != 200 : Couldn't retrieve content: Response code was 404 (expected 200)
Ответы (1 шт):
Автор решения: apisland
→ Ссылка
По сути это тест на внимательность, нужно привести логику проекта в соответствии с тестами path('second_page/', views.second_page, name='second_page') в urls.py и def second_page(request): return HttpResponse('А это вторая страница!') добавить "!" во views.py