django框架从零开始_010_自动化测试

参考:https://docs.djangoproject.com/en/1.9/intro/tutorial05/


为什么需要自动化测试?因为这可以在你的程序修改的时候告诉你是否对原先功能产生影响。不同于之前的手动测试,自动化测试是你预先创建一部分例子,在你修改代码后运行这些例子然后检查结果是否仍是你需要的。这样就可以省去每一次手工测试的时间。在实际生产中,这种自动化测试是必不可少的。关于自动化测试的重要性这里就不再赘述了。

自动化测试的策略有很多。有些程序员愿意遵从一种“test-driven”的方法,即在写程序之前,先定义好一些测试用例,然后实现你的代码逻辑去解决他们。这似乎有些顺序上的不对劲,但是实际上这和用户的使用方式是相同的:描述一个问题,然后解决他。然而更多的新人是先写好一部分代码,然后觉得他们应该写一些测试用例。不管怎样,现在开始做测试都不算晚。

有时很难决定要从哪里开始进行测试。如果你已经有了几千行代码,那测试的确是个费劲的活儿。在这种情况下,你的测试可以添加在下一次你的代码改动的时候,比如添加新功能或者fix bugs。对于我们这个小程序,让我们现在就开始建立第一个测试。


正好,我们之前的代码存在一些小bug。比如,在Question.was_published_recently()方法中,如果question是新建于今天的话会返回True,但是如果这个时间是在未来的话也会返回True(虽然实际不该发生)。我们可以通过控制台来确认一下:

>>> import datetime
>>> from django.utils import timezone
>>> from polls.models import Question
>>> # create a Question instance with pub_date 30 days in the future
>>> future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30))
>>> # was it published recently?
>>> future_question.was_published_recently()
True

这应该是错误的,让我们来建立测试用例来显露出这个bug。

tests.py是一个方便的建立apps测试用例的地方,整个自动测试系统会自动寻找所有的以test开头的方法来确定测试用例。

更新tests.py:

from django.test import TestCase

import datetime
from django.utils import timezome
from .models import Question

# Create your tests here.

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 = Question(pub_date=time)
        
        self.assertEqual(future_question.was_published_recently(), False)

在这里,我们建立了 django.test.TestCase的子类,在其中建立了一个测试方法新建了未来时间的question,并检查他的was_published_recently()方法应该返回False。


OK,我们来运行测试。运行方法为在cmd下:

$ python manage.py test polls
我们来看一下结果:


额,看来django用户没有建立数据库的权限。给django用户建立数据库的权限之后我们再次运行测试:



可以看到,测试用例抛出了错误,并详细指出了哪一行、哪个方法出现了错误。

好,下面我们来修复这个错误:

polls/models.py:

class Question(models.Model):
    ......
    def was_published_recently(self):
        return timezone.now() > self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    ......

再次测试:

很好!而且即使项目再复杂,我们也不用担心这个错误再次出现了。


下面我们可以继续添加更多的测试用例:

from django.test import TestCase

import datetime
from django.utils import timezone
from .models import Question

# Create your tests here.

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 = Question(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=3)
        future_question = Question(pub_date=time)
        self.assertEqual(future_question.was_published_recently(), False)
        
    def test_was_published_recently_with_recent_question(self):
        '''
        was_published_recently() should return False for questions whose pub_date is older than 1 day.
        '''
        time = timezone.now() - datetime.timedelta(hours=3)
        future_question = Question(pub_date=time)
        self.assertEqual(future_question.was_published_recently(), True)
        

显然,这些结果都是对的。



好了,有了这三个例子,我们可以保证,这个方法对以前、现在、将来的question都会正常工作了。


关于更多django.test.TestCase用法,请参考https://docs.djangoproject.com/en/1.9/topics/testing/tools/#django.test.TestCase

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值