Django 1.5.4 专题14 Basic unit testing
来源:互联网 发布:百度的算法 编辑:程序博客网 时间:2024/05/18 00:27
一.首先安装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 passwhen you run "manage.py test".Replace this with more appropriate tests for your application."""from django.test import TestCasefrom article.models import Article, get_upload_file_namefrom django.utils import timezonefrom time import time from django.core.urlresolvers import reverseclass 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
- Unit Testing
- unit testing
- Unit Testing
- Unit testing
- Unit Testing
- unit testing private methods
- Advanced Unit Testing
- unit testing in gamedev
- Unit testing with CPPUnit
- SQL Unit Testing
- 理解单元测试(Unit Testing)
- Unit Testing in Android
- 单元测试(Unit Testing)基础
- Unit Testing in JUnit
- Unit Testing with wxPython
- Unit testing support(译文)
- 【Android】unit testing 单元测试
- static的明显作用
- CCSequence
- Android 下拉刷新框架实现
- MATLAB排序函数
- 阿帕奇HTTP服务器下载与安装
- Django 1.5.4 专题14 Basic unit testing
- jquery-ajax-getscript
- 各PHP框架 性能测试(Yaf 2.1 Benchmark)
- jquery-ajax-ajax
- 联想u310 u410已组好RAID即启用了IRST而因意外不能进入系统要(重装系统方法)
- wordpress博客文章中外链添加nofollow属性
- .NET/ASP.NET MVC(模块化开发AraeRegistration)
- 关于const的一些知识
- java.lang.ClassCastException: oracle.sql.BLOB cannot be cast to oracle.sql.BLOB