Django后测试过程中的错误



我正试图在Django polls教程应用程序中测试递增投票。

我在django-shell和test.py中做了两个相同的测试。它们都不起作用,结果也不一样。该应用程序运行良好。

在第一种情况下,post请求的响应是200,没有递增值。在第二种情况下是"404"!对于与第一种情况相同的命令和Choice对象的错误。

这是两种情况:

*1( django外壳:

代码:

#python manage.py shell < 1.py
#>>> exec(open('1.py').read())
from django.test.utils import setup_test_environment
setup_test_environment()
from django.test import Client
client = Client()
from polls.models import Choice
print("votes before",Choice.objects.get(id=7).votes)
response = client.post('/polls/3/vote/', {'name':'choice','value':'7'} )
print(response.status_code)
response = client.get('/polls/3/results/')
print(response.content)
print("votes after",Choice.objects.get(id=7).votes)

结果:

votes before 5
200
b'<h1>What do you like?</h1>nn<ul>nn    <li>Coffee -- 5 votes</li>nn    <li>Coca Cola -- 4 votes</li>nn</ul>nn<a href="/polls/3/">Vote again?</a>n'
votes after 5

*2(django测试:

代码:

from .models import Choice
class TestVote(TestCase):
def test_if_vote_is_incrementing1(self):
response = self.client.post( '/polls/3/vote/', data = {'name':'choice','value':'7'} )
self.assertEqual(response.status_code, 200)
def test_if_vote_is_incrementing2(self):
votes0 = Choice.objects.get(id=7).votes
response = self.client.post( '/polls/3/vote/', data = {'name':'choice','value':'7'} )
votes1 = Choice.objects.get(id=7).votes
self.assertEqual(votes1, votes0+1)

结果:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
FE
======================================================================
ERROR: test_if_vote_is_incrementing2 (polls.tests.TestVote)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mihai/all/data/work2020/django/mysite4/polls/tests.py", line 13, in test_if_vote_is_incrementing2
votes0 = Choice.objects.get(id=7).votes
File "/home/mihai/env_dj/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/mihai/env_dj/lib/python3.7/site-packages/django/db/models/query.py", line 417, in get
self.model._meta.object_name
polls.models.Choice.DoesNotExist: Choice matching query does not exist.
======================================================================
FAIL: test_if_vote_is_incrementing1 (polls.tests.TestVote)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mihai/all/data/work2020/django/mysite4/polls/tests.py", line 10, in test_if_vote_is_incrementing1
self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200
----------------------------------------------------------------------
Ran 2 tests in 0.006s
FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...

已解决:

  1. shell命令如下:

    #python manage.py shell<1.py#>gt>exec(open('1.py'(.read(((

    从django.test.utils导入setup_test_environmentsetup_test_environment((从django.test导入客户端client=client((

    从polls.models导入Choice

    打印("之前的投票",Choice.objects.get(id=7(.pots(

    response=client.post('/polls/3/vote/',{选择':'7'}(打印(响应.status_code(

    response=client.get('/polls/3/results/'(打印(响应.内容(

    打印("后投票",Choice.objects.get(id=7(.vots(

  2. Django test.py必须是这样的:

    来自.models导入选项,问题从django.utils导入时区

    "quot"在测试模式下,Django生成一个需要初始化的临时空数据库"quot">

    def initDataBase((:q=问题(Question_text="你喜欢什么?",pub_date=时区.now(((q.save((q.choice_set.create(choice_text="投票人",投票数=0(q_choice_set.create(choice_text='Coca-Cola',投票数=0(

    类TestVote(TestCase(:def test_if_vote_is_incrementing1(self(:initDataBase((q=问题.objects.all(([0]c=Choice.objects.filter(Choice_text='Coffee'([0]url='/polls/'+str(q.id(+'/pvote/'数据={选择:str(c.id(}response=self.client.post(url,data(self.assertEqual(response.status_code,302(

    def test_if_vote_is_incrementing2(self):
    initDataBase()
    q = Question.objects.all()[0]
    c = Choice.objects.filter(choice_text='Coffee')[0]
    votes0 = Choice.objects.get(id=c.id).votes
    url = '/polls/'+str(q.id)+'/vote/'
    data = {'choice':str(c.id)}
    response = self.client.post(url, data )
    votes1 = Choice.objects.get(id=c.id).votes
    

相关内容

最新更新