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报表

效果如下

Django 1.5.4 专题14 Basic unit testing_第1张图片

你可能感兴趣的:(django)