什么是网站流量优化,四川住房建设厅网站,宜昌哪里有专业做网站的,石家庄住建局官方网查询一.前言/准备 测Django的东西仅限于在MTV模型。哪些可以测#xff1f;哪些不可以。
1.html里的东西不能测。①Html里的HTML代码大部分都是写死的②嵌套在html中的Django模板语言也不能测#xff0c;即使有部分逻辑。 但写测试用例时至少要调用一个类或者方法。模板语言没有出… 一.前言/准备 测Django的东西仅限于在MTV模型。哪些可以测哪些不可以。
1.html里的东西不能测。①Html里的HTML代码大部分都是写死的②嵌套在html中的Django模板语言也不能测即使有部分逻辑。 但写测试用例时至少要调用一个类或者方法。模板语言没有出参也没有入参不能测2.models模型可测。属于数据库层3.views视图层可以测。有入参、有方法。综上根据Django语言特点可测models和views 二.Django单元测试具体步骤----【测试模型models中的内容】
# codingutf-8from django.test import TestCase #导入Django测试包from sign.models import Guest, Event #导入models中的发布会、嘉宾类#首先创建测试类class ModelTest(TestCase): #初始化分别创建一条发布会Event和一条嘉宾Guest的数据。 def setUp(self): Event.objects.create(id1, nameoneplus 3 event, statusTrue, limit2000, addressshenzhen, start_time2016-08-31 02:18:22) Guest.objects.create(id1, event_id1, realnamealen, phone13711001101, emailalenmail.com, signFalse) #下面开始写测试用例了 #1.通过get的方法查询插入的发布会数据并根据地址判断 def test_event_models(self): result Event.objects.get(nameoneplus 3 event) self.assertEqual(result.address, shenzhen) self.assertTrue(result.status) #2.通过get的方法查询插入的嘉宾数据并根据名字判断 def test_guest_models(self): result Guest.objects.get(phone13711001101) self.assertEqual(result.realname, alen) self.assertFalse(result.sign) #写完测试用例后执行测试用例。这里与unittest的运行方法也不一样。 #Django提供了“test”命令来运行测试。用cmd执行 见下截图 注意刚刚在setup()部分操作时其实并不会真正向数据库表中插入数据。所以不用担心测试完后产生垃圾数据的问题。 如果插入了表中没有定义的字段时也是会报错提醒的 三。Django测试框架首选方法是使用python标准库中的unittest模块。 尽早进行单元测试UnitTest是比较好的做法极端的情况甚至强调“测试先行”。现在我们已经有了第一个model类和Form类是时候开始写测试代码了。
Django支持python的单元测试unit test和文本测试doc test我们这里主要讨论单元测试的方式。这里不对单元测试的理论做过多的阐述假设你已经熟悉了下列概念test suite, test case, test/test action, test data assert等等。
在单元测试方面Django继承python的unittest.TestCase实现了自己的django.test.TestCase编写测试用 例通常从这里开始。测试代码通常位于app的tests.py文件中(也可以在models.py中编写但是我不建议这样做。在Django生成的 depotapp中已经包含了这个文件并且其中包含了一个测试用例的样例
depot/depotapp/tests.py ? 1 2 3 4 5 6 7 from django.test import TestCase class SimpleTest(TestCase): def test_basic_addition(self): Tests that 1 1 always equals 2. self.assertEqual(1 1,2) 你可以有几种方式运行单元测试
python manage.py test执行所有的测试用例python manage.py test app_name, 执行该app的所有测试用例python manage.py test app_name.case_name: 执行指定的测试用例
用第三种方式执行上面提供的样例结果如下 ? 1 $ python manage.pytest depotapp.SimpleTest ? 1 2 3 4 5 6 7 Creating test database for alias default... . ---------------------------------------------------------------------- Ran 1 test in 0.012s OK Destroying test database for alias default... 你可能会主要到输出信息中包括了创建和删除数据库的操作。为了避免测试数据造成的影响测试过程会使用一个单独的数据库关于如何指定测试数据库 的细节请查阅Django文档。在我们的例子中由于使用sqlite数据库Django将默认采用内存数据库来进行测试。
下面就让我们来编写测试用例。在《Agile Web Development with Rails 4th》中7.2节最终实现的ProductTest代码如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 class ProductTest ActiveSupport::TestCase test product attributes must not be emptydo product Product.new assert product.invalid? assert product.errors[:title].any? assert product.errors[:description].any? assert product.errors[:price].any? assert product.errors[:image_url].any? end test product price must be positivedo product Product.new(:title My Book Title, :description yyy, :image_url zzz.jpg) product.price -1 assert product.invalid? assert_equal must be greater than or equal to 0.01, product.errors[:price].join(; ) product.price 0 assert product.invalid? assert_equal must be greater than or equal to 0.01, product.errors[:price].join(; ) product.price 1 assert product.valid? end def new_product(image_url) Product.new(:title My Book Title, :description yyy, :price 1, :image_url image_url) end test image urldo ok %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg http://a.b.c/x/y/z/fred.gif } bad %w{ fred.doc fred.gif/more fred.gif.more } ok.eachdo |name| assert new_product(name).valid?, #{name} shouldnt be invalid end bad.eachdo |name| assert new_product(name).invalid?, #{name} shouldnt be valid end end test product is not valid without a unique titledo product Product.new(:title products(:ruby).title, :description yyy, :price 1, :image_url fred.gif) assert !product.save assert_equal has already been taken, product.errors[:title].join(; ) end test product is not valid without a unique title - i18ndo product Product.new(:title products(:ruby).title, :description yyy, :price 1, :image_url fred.gif) assert !product.save assert_equal I18n.translate(activerecord.errors.messages.taken), product.errors[:title].join(; ) end end 对Product测试的内容包括
1.titledescriptionpriceimage_url不能为空
2. price必须大于零
3. image_url必须以jpgpngjpg结尾并且对大小写不敏感
4. titile必须唯一
让我们在Django中进行这些测试。由于ProductForm包含了模型校验和表单校验规则使用ProductForm可以很容易的实现上述测试
depot/depotapp/tests.py ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #/usr/bin/python #coding: utf8 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 forms import ProductForm class SimpleTest(TestCase): def test_basic_addition(self): Tests that 1 1 always equals 2. self.assertEqual(1 1,2) class ProductTest(TestCase): def setUp(self): self.product { title:My Book Title, description:yyy, image_url:http://google.com/logo.png, price:1 } f ProductForm(self.product) f.save() self.product[title] My Another Book Title #### titledescriptionpriceimage_url不能为空 def test_attrs_cannot_empty(self): f ProductForm({}) self.assertFalse(f.is_valid()) self.assertTrue(f[title].errors) self.assertTrue(f[description].errors) self.assertTrue(f[price].errors) self.assertTrue(f[image_url].errors) #### price必须大于零 def test_price_positive(self): f ProductForm(self.product) self.assertTrue(f.is_valid()) self.product[price] 0 f ProductForm(self.product) self.assertFalse(f.is_valid()) self.product[price] -1 f ProductForm(self.product) self.assertFalse(f.is_valid()) self.product[price] 1 #### image_url必须以jpgpngjpg结尾并且对大小写不敏感 def test_imgae_url_endwiths(self): url_base http://google.com/ oks (fred.gif,fred.jpg, fred.png,FRED.JPG, FRED.Jpg) bads (fred.doc,fred.gif/more, fred.gif.more) for endwith in oks: self.product[image_url] url_baseendwith f ProductForm(self.product) self.assertTrue(f.is_valid(),msgerror when image_url endwith endwith) for endwith in bads: self.product[image_url] url_baseendwith f ProductForm(self.product) self.assertFalse(f.is_valid(),msgerror when image_url endwith endwith) self.product[image_url] http://google.com/logo.png ### titile必须唯一 def test_title_unique(self): self.product[title] My Book Title f ProductForm(self.product) self.assertFalse(f.is_valid()) self.product[title] My Another Book Title 然后运行 python manage.py test depotapp.ProductTest。如同预想的那样测试没有通过 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Creating test database for alias default... .F.. FAIL: test_imgae_url_endwiths (depot.depotapp.tests.ProductTest) ---------------------------------------------------------------------- Traceback (most recent call last): File /Users/holbrook/Documents/Dropbox/depot/../depot/depotapp/tests.py, line 65, in test_imgae_url_endwiths self.assertTrue(f.is_valid(),msgerror when image_url endwith endwith) AssertionError: False is not True : error when image_url endwith FRED.JPG ---------------------------------------------------------------------- Ran 4 tests in 0.055s FAILED (failures1) Destroying test database for alias default... 因为我们之前并没有考虑到image_url的图片扩展名可能会大写。修改ProductForm的相关部分如下 ? 1 2 3 4 5 def clean_image_url(self): url self.cleaned_data[image_url] ifnot endsWith(url.lower(),.jpg, .png,.gif): raise forms.ValidationError(图片格式必须为jpg、png或gif) return url 然后再运行测试 ? 1 $ python manage.pytest depotapp.ProductTest ? 1 2 3 4 5 6 7 Creating test database for alias default... .... ---------------------------------------------------------------------- Ran 4 tests in 0.060s OK Destroying test database for alias default... 测试通过并且通过单元测试我们发现并解决了一个bug。