我想用pytest来测试我正在处理的Web应用程序。我开始小:404错误与pytest'manage.py外壳'和浏览器不
def test_loading_page(client):
response = client.get('/')
assert response.status_code == 200
assert b'Congratulations on your first Django' in response.content
根据pytest的输出,这看起来是如预期运行:
platform win32 -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
Django settings: test_project.settings (from ini file)
rootdir: C:\--\---\---\---\---\---, inifile: pytest.ini
plugins: django-3.1.2
collected 1 items
test_project.py F
================================== FAILURES ===================================
______________________________ test_loading_page _______________________________
client = <django.test.client.Client object at 0x000002440F0A17F0>
def test_loading_page(client):
response = client.get('/')
> assert response.status_code == 200
E assert 404 == 200
E + where 404 = <HttpResponseNotFound status_code=404, "text/html">.status_code
test_test_project.py:7: AssertionError
========================== 1 failed in 0.36 seconds ===========================
所以Django的开发服务器运行时,test_loading_page失败,因为的404 。如果使用我的浏览器去http://127.0.0.1:8000/显示加载页面。另外,如果我使用Python manage.py壳,下面的代码运行正常:
from django.test import Client
client = Client()
response = client.get('/')
response.status_code == 200 #Returns True
在情况下,它是有用的,但pytest.ini文件包含:
[pytest]
DJANGO_SETTINGS_MODULE = flawlesslinguistics.settings
而且urls.py文件默认:
从django.conf.urls导入网址 从django.contrib中导入管理
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
个版本我使用的是:
的Python 3.6.1
pytest-3.0.7
Django的3.1.2
很难确定哪些文章/文档是指pyte st和哪些是django的内置测试框架,使用unittest。任何专家的帮助将不胜感激。
什么是你的urls.py? – noes1s
已被加入到问题中noes1s – user3535074