python第三方库系列之十八--python/django test库
django是属于python语音的web框架,要说django測试。也能够先说说python的測试。django能够用python的方式測试,当然,django也基于python封装了一个自己的測试库。
一、python的測试--unitest库
(1)django.utils.unittest库
更高级一点的,引用django封装的unittest。对了,这样写另一个优点:測试case能够直接用django里的models,操作数据库很方便。
django作为web的服务端,要測试其服务功能,必须模拟一个client,訪问服务端验证
一、python的測试--unitest库
def my_func(a_list, idx): return a_list[idx] import unittest class MyFuncTestCase(unittest.TestCase): def testBasic(self): a = ['larry', 'curly', 'moe'] self.assertEqual(my_func(a, 0), 'larry') self.assertEqual(my_func(a, 1), 'curly')二、django的測试--django.utils.unittest库、django.test.client库。
(1)django.utils.unittest库
更高级一点的,引用django封装的unittest。对了,这样写另一个优点:測试case能够直接用django里的models,操作数据库很方便。
from django.utils import unittest from myapp.models import Animal class AnimalTestCase(unittest.TestCase): def setUp(self): self.lion = Animal.objects.create(name="lion", sound="roar") self.cat = Animal.objects.create(name="cat", sound="meow") def test_animals_can_speak(self): """Animals that can speak are correctly identified""" self.assertEqual(self.lion.speak(), 'The lion says "roar"') self.assertEqual(self.cat.speak(), 'The cat says "meow"')(2)django.test.client库
django作为web的服务端,要測试其服务功能,必须模拟一个client,訪问服务端验证
from django.utils import unittest from django.test.client import Client class SimpleTest(unittest.TestCase): def setUp(self): # Every test needs a client. self.client = Client() def test_details(self): # Issue a GET request. response = self.client.get('/customer/details/') # Check that the response is 200 OK. self.assertEqual(response.status_code, 200) # Check that the rendered context contains 5 customers. self.assertEqual(len(response.context['customers']), 5)或者更高级的。直接用from django.test.TestCase
from django.test import TestCase class SimpleTest(TestCase): def test_details(self): response = self.client.get('/customer/details/') self.assertEqual(response.status_code, 200) def test_index(self): response = self.client.get('/customer/index/') self.assertEqual(response.status_code, 200)三、数据库使用
假设你測试时候还须要初始化数据库,那TestCase.fixtures帮你了。
并且,django在測试的时候。会帮你创建一个新的数据库名为:test_原有数据库名。
这样能够防止真实数据库被弄脏。
你能够这样初始化你的数据库:from django.test import TestCase from myapp.models import Animal class AnimalTestCase(TestCase): fixtures = ['mammals.json', 'birds'] def setUp(self): # Test definitions as before. call_setup_methods() def testFluffyAnimals(self): # A test that uses the fixtures. call_some_test_code()那有人就问了:mammals.json怎么来的呢?格式是什么呢?事实上这个文件是用python manage.py dumpdata命令来的。有关这个命令的文档请google之。文件格式大概例如以下:
[ { "pk": 1, "model": "apps.animal", "fields": { "status": 1, "gmt_modified": "2015-07-06T14:05:38", "gmt_created": "2015-07-06T14:05:38", "alive": 1, "info": "" } }, { "pk": 2, "model": "apps.animal", "fields": { "status": 0, "gmt_modified": "2015-07-06T14:05:53", "gmt_created": "2015-07-06T14:05:53", "alive": 1, "info": "" } } ]事实上,django.test这个包还有非常多其它的功能,不能一一列举,具体能够看django官方文档。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步