Django L5 编写你的第一个Django应用

http://python.usyiyi.cn/translate/django_182/intro/tutorial05.html

 

自动化测试简介

为什么你需要创建测试

 

测试不仅可以发现问题还可以防止问题

测试使你的代码更受欢迎

测试有助于团队工作

 

基本的测试策略

 

测试驱动开发可以用Python测试用例将这个问题简单地形式化。

  创建一个测试来暴露这个问题

 

$ cat tasks/tests.py
from django.test import TestCase

# Create your tests here.
import datetime

from django.utils import timezone
from django.test import TestCase

from .models import Questions


class QuestionMethodTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() should return False for questions whose
        pub_date is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Questions(pub_date=time)
        self.assertEqual(future_question.was_published_recently(), False)

  

$ python manage.py test tasks
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_was_published_recently_with_future_question (tasks.tests.QuestionMethodTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ly/PycharmProjects/ltest/tasks/tests.py", line 21, in test_was_published_recently_with_future_question
    self.assertEqual(future_question.was_published_recently(), False)
AssertionError: True != False

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)
Destroying test database for alias 'default'...

  

修复这个错误

class Questions(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('data published')

    '''
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    '''
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now
    def __unicode__(self):      #
        return self.question_text

  

更加综合的测试

$ cat tasks/tests.py
from django.test import TestCase

# Create your tests here.
import datetime

from django.utils import timezone
from django.test import TestCase

from .models import Questions


class QuestionMethodTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() should return False for questions whose
        pub_date is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Questions(pub_date=time)
        self.assertEqual(future_question.was_published_recently(), False)

    def test_was_published_recently_with_old_question(self):
        """
        was_published_recently() should return False for questions whose
        pub_date is older than 1 day.
        """
        time = timezone.now() - datetime.timedelta(days=30)
        old_question = Questions(pub_date=time)
        self.assertEqual(old_question.was_published_recently(), False)

    def test_was_published_recently_with_recent_question(self):
        """
        was_published_recently() should return True for questions whose
        pub_date is within the last day.
        """
        time = timezone.now() - datetime.timedelta(hours=1)
        recent_question = Questions(pub_date=time)
        self.assertEqual(recent_question.was_published_recently(), True)

  应用虽然简单,但是无论它今后会变得多么复杂以及会和多少其它的应用产生相互作用,我们都能保证我们刚刚为它编写过测试的那个方法会按照预期的那样工作。

 

测试一个视图

 

视图的一个测试

Django 测试客户端

>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()

 

改进视图

 

测试DetailView

 

测试越多越好

 

 

 

  

 

posted @ 2017-02-10 15:29  ZSR0401  阅读(182)  评论(0编辑  收藏  举报