简体   繁体   English

Django 测试程序后的错误

[英]Errors in post Django testing procedure

I'm trying to make a test for incrementig votes in Django polls tutorial application.我正在尝试在 Django 投票教程应用程序中对增量投票进行测试。

I made two identical tests in django shell and in test.py.我在 django shell 和 test.py 中做了两个相同的测试。 None of them working and results are different.他们都没有工作和结果是不同的。 The app is working well.该应用程序运行良好。

In the 1st case response of post request is 200, no incrementing value.在第一种情况下,post 请求的响应是 200,没有递增值。 In the 2nd case a '404'.在第二种情况下是“404”。 for same command as the 1st case and error for Choice object.对于与第一种情况相同的命令和选择对象的错误。

These are the two situations:这是两种情况:

*1) django shell: *1) django shell:

Code:代码:

#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)

Result:结果:

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

*2) django test: *2) django 测试:

Code:代码:

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)

Result:结果:

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'...

Solved:解决了:

  1. shell commands will be like that: shell 命令将如下所示:

    #python manage.py shell < 1.py #>>> exec(open('1.py').read()) #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 django.test.utils import setup_test_environment setup_test_environment() from django.test import Client client = Client()

    from polls.models import Choice从 polls.models 导入选择

    print("votes before",Choice.objects.get(id=7).votes) print("投票前",Choice.objects.get(id=7).votes)

    response = client.post('/polls/3/vote/', {'choice':'7'} ) print(response.status_code) response = client.post('/polls/3/vote/', {'choice':'7'} ) print(response.status_code)

    response = client.get('/polls/3/results/') print(response.content) response = client.get('/polls/3/results/') print(response.content)

    print("votes after",Choice.objects.get(id=7).votes) print("投票后",Choice.objects.get(id=7).votes)

  2. Django test.py must look like that: Django test.py 必须如下所示:

    from.models import Choice, Question from django.utils import timezone from.models 导入选择,来自 django.utils 导入时区的问题

    """ In testing mode Django generates a temporary empty database which needs initialization """ """ 在测试模式下 Django 生成一个需要初始化的临时空数据库 """

    def initDataBase(): q = Question(question_text="What do you like?", pub_date=timezone.now()) q.save() q.choice_set.create(choice_text='Coffee', votes=0) q.choice_set.create(choice_text='Coca-Cola', votes=0) def initDataBase(): q = Question(question_text="你喜欢什么?", pub_date=timezone.now()) q.save() q.choice_set.create(choice_text='Coffee', votes=0) q. choice_set.create(choice_text='Coca-Cola', votes=0)

    class TestVote(TestCase): def test_if_vote_is_incrementing1(self): initDataBase() q = Question.objects.all()[0] c = Choice.objects.filter(choice_text='Coffee')[0] url = '/polls/'+str(q.id)+'/vote/' data = {'choice':str(c.id)} response = self.client.post(url, data ) self.assertEqual(response.status_code, 302) class TestVote(TestCase): def test_if_vote_is_incrementing1(self): initDataBase() q = Question.objects.all()[0] c = Choice.objects.filter(choice_text='Coffee')[0] url = '/polls/ '+str(q.id)+'/vote/' data = {'choice':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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM