2017-10-17 134 views
0

我想用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。任何专家的帮助将不胜感激。

+0

什么是你的urls.py? – noes1s

+0

已被加入到问题中noes1s – user3535074

回答

0

如果这样的:

def test_loading_page(client): 
    response = client.get('/') 
    assert response.status_code == 200 
    assert b'Congratulations on your first Django' in response.content 

是你views.py那么你有没有通过对urls.py

urls.py

from django.conf.urls import url 

from . import views 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^test_loading_page/$', views.test_loading_page), 
] 

根据总的映射这一观点设置你的应用程序,无论你在主项目中有多个urls.py,还有一个在myapp中。您可能需要“包含”并通过网址映射。

+0

是的,当我询问关于urls.py并且得出同样的结论时,我遵循了你的线索。由于占位符内容出现在屏幕上,当没有实际设置视图时,我想像它将通过pytest以与开发视图相同的方式进行测试。我会接受你的回答。谢谢! – user3535074

+0

很高兴你知道了! – noes1s

0

我觉得Pytest和Django需要一些帮助才能一起工作。查看pytest-django插件以使其更容易。

+0

我已经安装了pytest-django。 – user3535074

0

基于noes1s的问题,我调查了urls.py链。

我在一个项目的应用程序增加了一个占位符视图和有线起来有以下变化:

Test_project_urls。PY

from django.conf.urls import include, url 
from django.contrib import admin 

urlpatterns = [ 
    url(r'^test_project_app/', include('test_project_app.urls')), 
    url(r'^admin/', admin.site.urls), 
] 

Test_project_app_urls.py

from django.conf.urls import url 
from . import views 

urlpatterns = [ 
    url(r'^$', views.index, name='index'), 
] 

Test_project_app_views.py

from django.http import HttpResponse 

def index(request): 
    return HttpResponse("Congratulations on your first Django") 

所以这些变化预期pytest工作。看起来这里的问题是,典型的默认django页面不能像创建自定义视图时那样访问,并且与urls.py文件联系在一起。

注最初的测试需要稍微改变,以工作打算:

def test_loading_page(client): 
    response = client.get('/test_project_app/') 
    assert response.status_code == 200 
    assert 'Congratulations on your first Django' in str(response.content) 
相关问题