2017-10-17 137 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) 
相關問題