Django 1.5.4 专题14 Basic unit testing

一.首先安装coverage

pip install coverage

二.Test app 运行,输出详细信息

python manage.py test article -v 2

三.修改aritcle/tests.py的内容如下

"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from article.models import Article, get_upload_file_name
from django.utils import timezone
from time import time 
from django.core.urlresolvers import reverse

class ArticleTest(TestCase):
    def create_article(self, title="test article", body="Blah Blah Blah"):
        return Article.objects.create(title=title, 
                                      body=body,
                                      pub_date=timezone.now(),
                                      likes=0)
    
    
    def test_article_creation(self):
        a = self.create_article()
        self.assertTrue(isinstance(a, Article))
        self.assertEqual(a.__unicode__(), a.title)
        

    def test_get_upload_file_name(self):
        filename = "Cheese.txt"
        path = "uploaded_files/%s_%s" % (str(time()).replace('.','_'), 
                                         filename) 
        
        created_path = get_upload_file_name(self, filename)
        
        self.assertEqual(path, created_path)
        
        
    def test_articles_list_view(self):
        a = self.create_article()
        url = reverse('article.views.articles')
        resp = self.client.get(url)
        
        self.assertEqual(resp.status_code, 200)
        self.assertIn(a.title, resp.content)
        
        
    def test_article_detail_view(self):
        a = self.create_article()
        url = reverse('article.views.article', args=[a.id])
        resp = self.client.get(url)
        
        self.assertEqual(reverse('article.views.article', args=[a.id]), 
                         a.get_absolute_url())
        self.assertEqual(resp.status_code, 200)
        self.assertIn(a.title, resp.content)        

四.运行coverage html

     coverage run manage.py test article -v 2

     coverage html

     在htmlcov中获取html报表

效果如下


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值