• Django单测详解


    Django单测详解

     

    编写单元测试

    Django的单元测试是基于Python的标准库模块:unittest实现的。

    下面我们使用一个例子来了解一下如何编写单测Case:

    1. from django.test import TestCase
    2. from myapp.models import Animal
    3.  
    4. class AnimalTestCase(TestCase):
    5.     def setUp(self):
    6.         Animal.objects.create(name="lion", sound="roar")
    7.         Animal.objects.create(name="cat", sound="meow")
    8.  
    9.     def test_animals_can_speak(self):
    10.         """Animals that can speak are correctly identified"""
    11.         lion = Animal.objects.get(name="lion")
    12.         cat = Animal.objects.get(name="cat")
    13.         self.assertEqual(lion.speak(), 'The lion says "roar"')
    14.         self.assertEqual(cat.speak(), 'The cat says "meow"')

    当你想要运行一个测试用例时,默认情况下会找到所有test开头的文件。

    查找这些文件中所有的测试用例:unittest.TestCase子类,并自动运行这些Case。

    Ps:在Django中编写单测Case时,应该引用django.test.TestCase而不是unittest.TestCase。

    接口层级的测试Case编写

    Django中,提供了Client类可以用于进行接口级的测试。

    1. from django.test import Client
    2. = Client()
    3. response = c.post('/login/', {'username': 'john', 'password': 'smith'})
    4. response.status_code
    5. # 200
    6. response = c.get('/customer/details/')
    7. print response.content

    默认情况下CSRF检查是被禁用的,如果测试需要,可以用下面的方法:

    1. from django.test import Client
    2. csrf_client = Client(enforce_csrf_checks=True)

    更常用的一种方法如下:

    1. from django.test import TestCase
    2.  
    3. class SimpleTest(TestCase):
    4.     def test_details(self):
    5.         response = self.client.get('/customer/details/')
    6.         self.assertEqual(response.status_code, 200)
    7.  
    8.     def test_index(self):
    9.         response = self.client.get('/customer/index/')
    10.         self.assertEqual(response.status_code, 200)

    执行测试Case

    当测试用例编写完成后,运行manage.py文件,参数为test即可执行测试用例:

    1. python manage.py test

    执行该命令后,默认会在整个项目下查找test开头的文件并自动执行。

    除了全局执行外,还可以指定特定文件或特定类进行运行,例如:

    1. ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak
    2. ./manage.py test animals.tests.AnimalTestCase
    3. ./manage.py test animals.tests
    4. ./manage.py test animals

    此外,还可以支持通过正则匹配参数来选择文件执行:

    1. ./manage.py test --pattern="tests_*.py"

    Ps:测试过程中,可以正常使用Ctrl+C来中断测试Case的运行。且不会影响数据销毁等功能。

    添加-Wall参数可以在运行测试Case时将警告信息打印出来:

    1. python -Wall manage.py test

    测试数据库

    单测Case运行过程中,难免会造成各种脏数据的产生。

    Django本身支持对于关系型数据库的数据回收。

    但是对于非关系型数据库,例如MongoDB,Redis等,则需要利用额外的工具来实现。

    推荐使用的库是:django-test-addons

    安装方式如下:

    1. pip install django-test-addons

    接下来,需要在配置文件中添加TEST_MONGO_DATABASE。

    示例如下:

    1. TEST_MONGO_DATABASE = {
    2.     'db': 'test',
    3.     'host': ['localhost'],
    4.     'port': 27017,
    5. }

    对于需要使用MongoDB数据的测试Case,编写Case的方式如下:

    1. import test_addons
    2.  
    3. class TestSomething(test_addons.MongoTestCase):
    4.     def test_instantiation(self):
    5.         pass

    与coverage.py集成

    Django可以轻松地与coverage.py集成,这是一个用于测量Python程序代码覆盖率的工具。

    首先,需要安装coverage.py。

    接下来,在mange.py所在的文件夹下执行如下命令:

    1. coverage run --source='.' manage.py test myapp

    这将运行您的测试并收集项目中已执行文件的coverage数据。

    最后,您可以通过键入以下命令查看此数据的报告:

    1. coverage report

    如果期望生成详细的html报告,可以执行如下命令:

    1. coverage html

  • 相关阅读:
    HDU 6181 Two Paths【次短路】【模板题】
    POJ 1236 Network of Schools【tarjan算法】【模板题】
    POJ 1236 Network of Schools【tarjan算法】【模板题】
    Tarjan 算法&模板
    Tarjan 算法&模板
    HDU 6168 Numbers【水题】
    HDU 6168 Numbers【水题】
    HDU 4523 湫秋系列故事——安排座位【组合dp】【好题】【思维题】
    HDU 4523 湫秋系列故事——安排座位【组合dp】【好题】【思维题】
    HDU 2087 剪花布条【最长不重复子串】【KMP】【水题】【模板题】
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/14874098.html
Copyright © 2020-2023  润新知