django的单元测试框架unittest、覆盖率
django的单元测试
指定测试范围:
- 指定运行某些测试文件./manage.py test --pattern="tests_*.py" -v 2
- 运行所有测试文件./manage.py test -v 2
-
# Run all the tests in the animals.tests module $ ./manage.py test animals.tests
-
# Run all the tests found within the 'animals' package $ ./manage.py test animals
-
# Run just one test case $ ./manage.py test animals.tests.AnimalTestCase
-
# Run just one test method $ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak
提高测试速度
Running tests in parallel
As long as your tests are properly isolated, you can run them in parallel to gain a speed up on multi-core hardware. See test --parallel
.
临时数据库:
运行时会临时创建数据库,运行结束后删除,数据库名字:test_用到的数据库。猜测是使用migragtion文件创建的。
使用选项--keepdb可以保留测试数据库
The test --keepdb
option preserves the test database between test runs. It skips the create and destroy actions which can greatly decrease the time to run tests.
举例:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @Author : @Date : 2019-01-02 22:48 @Description : 本文件的作用描述 @File : test_demo.py """ from unittest.mock import patch from django.test import TestCase import os def multiple(a, b): return a * b class Calculator(object): def add(self, a, b): return a+b def is_error(self): try: os.mkdir("11") return False except Exception as e: return True def multiple(self, a, b): return a * b # Mock一个函数。 class TestOneFunc(TestCase): # 指定函数的写法:要先写当前文件名,即module name @patch('test_demo.multiple') def test_multiple(self, mock_multiple): mock_multiple.return_value = 3 self.assertEqual(multiple(8, 14), 3) class TestProducer(TestCase): def setUp(self): self.calculator = Calculator() # Mock的函数每次被调用返回不同的值 @patch.object(Calculator, 'add') def test_effect(self, mock_add): # 注意list的顺序要和后面的顺序一一对应 mock_add.side_effect = [1, 2, 3] self.assertEqual(self.calculator.add(8, 14), 1) self.assertEqual(self.calculator.add(8, 14), 2) self.assertEqual(self.calculator.add(8, 14), 3) # Mock一个对象里面的方法 @patch.object(Calculator, 'add') def test_add1(self, mock_add1): # 模拟add函数的返回值为3,所以不管真实结果是否是3,都没有影响 mock_add1.return_value = 3 self.assertEqual(self.calculator.add(8, 14), 3) # 让Mock的函数抛出exception @patch('os.mkdir') def test_exception(self, mock_mkdir): mock_mkdir.side_effect = Exception self.assertEqual(self.calculator.is_error(), True) # Mock多个函数,主要是注意顺序 @patch.object(Calculator, 'add') @patch.object(Calculator, 'multiple') def test_both(self, mock_multiple, mock_add): mock_add.return_value = 1 mock_multiple.return_value = 2 self.assertEqual(self.calculator.add(8, 14), 1) self.assertEqual(self.calculator.multiple(8, 14), 2) class ClassTemplate(object): def __init__(self, name): # __name c是私有变量,只有类本身可以访问,子类也不可以访问 # _name 是受保护变量,类本身和子类可以访问,from module import *无法导入 # decorator/迭代器/yield/ self.__name = name def run(self): print("my name is %s" % self.__name) @staticmethod def staticmethod(value): print("my value is {}".format(value)) if __name__ == '__main__': ClassTemplate('a_name').run() ClassTemplate.staticmethod('a_value')
from unittest.mock import patch, Mock from django.test import TestCase, Client, RequestFactory import os # import requests import client def multiple(a, b): return a * b class Calculator(object): def add(self, a, b): return a+b def is_error(self): try: os.mkdir("11") return False except Exception as e: return True def multiple(self, a, b): return a * b # Mock一个函数。 class TestOneFunc(TestCase): def setUp(self): self.client = Client() self.request_factory = RequestFactory() # self.calculator = Calculator() # 指定函数的写法:要先写当前文件名,即module name @patch('test_demo.multiple') def test_multiple(self, mock_multiple): # https://docs.djangoproject.com/zh-hans/2.1/topics/testing/advanced/ # 主要用于构造request对象,与client对比,没有发生url接口的调用。 request = self.request_factory.post('/vpc/list', data={'start': 0, 'length': 10}) # 主要用于模仿url请求,调用并返回请求结果。 response = self.client.get('/vpc/list') mock_multiple.return_value = 3 self.assertEqual(multiple(8, 14), 3) class TestProducer(TestCase): def setUp(self): self.calculator = Calculator() # Mock的函数每次被调用返回不同的值 @patch.object(Calculator, 'add') def test_effect(self, mock_add): # 注意list的顺序要和后面的顺序一一对应 mock_add.side_effect = [1, 2, 3] self.assertEqual(self.calculator.add(8, 14), 1) self.assertEqual(self.calculator.add(8, 14), 2) self.assertEqual(self.calculator.add(8, 14), 3) # Mock一个对象里面的方法 @patch.object(Calculator, 'add') def test_add1(self, mock_add1): # 模拟add函数的返回值为3,所以不管真实结果是否是3,都没有影响 mock_add1.return_value = 3 self.assertEqual(self.calculator.add(8, 14), 3) # 让Mock的函数抛出exception @patch('os.mkdir') def test_exception(self, mock_mkdir): mock_mkdir.side_effect = Exception self.assertEqual(self.calculator.is_error(), True) # Mock多个函数,主要是注意顺序 @patch.object(Calculator, 'add') @patch.object(Calculator, 'multiple') def test_both(self, mock_multiple, mock_add): mock_add.return_value = 1 mock_multiple.return_value = 2 self.assertEqual(self.calculator.add(8, 14), 1) self.assertEqual(self.calculator.multiple(8, 14), 2) class TestClient(TestCase): def test_success_request(self): success_send = Mock(return_value='200') client.send_request = success_send self.assertEqual(client.visit_ustack(), '200') def test_fail_request(self): fail_send = Mock(return_value='404') client.send_request = fail_send self.assertEqual(client.visit_ustack(), '404') class TestClient1(TestCase): def test_call_send_request_with_right_arguments(self): client.send_request = Mock() client.visit_ustack() # Mock对象的called属性表示该mock对象是否被调用过。 self.assertEqual(client.send_request.called, True) # Mock对象的call_args表示该mock对象被调用的tuple,tuple的每个成员都是一个mock.call对象。 # mock.call这个对象代表了一次对mock对象的调用,其内容是一个tuple,含有两个元素, # 第一个元素是调用mock对象时的位置参数(*args),第二个元素是调用mock对象时的关键字参数( ** kwargs) call_args = client.send_request.call_args self.assertIsInstance(call_args[0][0], str) # 使用patch或者patch.object的目的是为了控制mock的范围,意思就是在一个函数范围内, # 或者一个类的范围内,或者with语句的范围内mock掉一个对象 class TestClient2(TestCase): def test_success_request(self): status_code = '200' success_send = Mock(return_value=status_code) with patch('client.send_request', success_send): from client import visit_ustack self.assertEqual(visit_ustack(), status_code) # patch用法 def test_fail_request(self): status_code = '404' fail_send = Mock(return_value=status_code) with patch('client.send_request', fail_send): from client import visit_ustack self.assertEqual(visit_ustack(), status_code) # patch.object用法 def test_fail_request2(self): status_code = '404' fail_send = Mock(return_value=status_code) with patch.object(client, 'send_request', fail_send): from client import visit_ustack self.assertEqual(visit_ustack(), status_code) class ClassTemplate(object): def __init__(self, name): # __name c是私有变量,只有类本身可以访问,子类也不可以访问 # _name 是受保护变量,类本身和子类可以访问,from module import *无法导入 # decorator/迭代器/yield/ self.__name = name @classmethod def class_method(cls): cls.staticmethod('test') def run(self): print("my name is %s" % self.__name) @staticmethod def staticmethod(value): print("my value is {}".format(value)) if __name__ == '__main__': ClassTemplate('a_name').run() ClassTemplate.staticmethod('a_value') ClassTemplate.class_method()
覆盖率
- 安装coverage包:
pip install coverage
- 执行用例
coverage run --source '.' manage.py test -v 2
- 查看覆盖率:
macbook:portal zhaoxueyong$ coverage report
Mock
Mock对象的一般用法是这样的:
-
找到你要替换的对象,这个对象可以是一个类,或者是一个函数,或者是一个类实例。
-
然后实例化Mock类得到一个mock对象,并且设置这个mock对象的行为,比如被调用的时候返回什么值,被访问成员的时候返回什么值等。
-
使用这个mock对象替换掉我们想替换的对象,也就是步骤1中确定的对象。
-
之后就可以开始写测试代码,这个时候我们可以保证我们替换掉的对象在测试用例执行的过程中行为和我们预设的一样。
- 可以替换自己写的模块的对象,其实也可以替换标准库和第三方模块的对象,方法是一样的:先import进来,然后替换掉指定的对象就可以了
Client vs RequestFactory
from django.test import TestCase, Client, RequestFactory
RequestFactory
and Client
have some very different use-cases. To put it in a single sentence: RequestFactory
returns a request
, while Client
returns a response
.
The RequestFactory
does what it says - it's a factory to create request
objects. Nothing more, nothing less.
The Client
is used to fake a complete request-response cycle. It will create a request
object, which it then passes through a WSGI handler. This handler resolves the url, calls the appropriate middleware, and runs the view. It then returns the response object. It has the added benefit that it gathers a lot of extra data on the response
object that is extremely useful for testing.
The RequestFactory
doesn't actually touch any of your code, but the request
object can be used to test parts of your code that require a valid request
. The Client
runs your views, so in order to test your views, you need to use the Client
and inspect the response. Be sure to check out the documentation on the Client
.
测试用例执行前后的工作:
class TestMathFunc(unittest.TestCase): """Test mathfuc.py""" # 所有测试用例执行前执行一次 @classmethod def setUpClass(cls): print "This setUpClass() method only called once." # 所有测试用例执行后执行一次 @classmethod def tearDownClass(cls): print "This tearDownClass() method only called once too."
# 每个用例前执行一次
def setUp(self):
print "do something before test.Prepare environment."
# 每个用例后执行一次
def tearDown(self):
print "do something after test.Clean up."
@unittest.skip("I don't want to run this case.")
def test_divide(self):
"""Test method divide(a, b)"""
print "divide"
self.assertEqual(2, divide(6, 3))
self.assertEqual(2.5, divide(5, 2))
测试套以及html输出报告:
import unittest from test_mathfunc import TestMathFunc from HTMLTestRunner import HTMLTestRunner if __name__ == '__main__': suite = unittest.TestSuite() suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestMathFunc)) with open('HTMLReport.html', 'w') as f: runner = HTMLTestRunner(stream=f, title='MathFunc Test Report', description='generated by HTMLTestRunner.', verbosity=2 ) runner.run(suite)
test --pattern="test_plugin_check.py" -v 3 --debug-mode --debug-sql --parallel 2
参考:
1、https://www.jianshu.com/p/b87d7b4a222a
2、https://segmentfault.com/a/1190000002965620
3、https://docs.python.org/3.7/library/unittest.mock.html
4、https://docs.djangoproject.com/zh-hans/2.1/topics/testing/overview/
5、https://docs.djangoproject.com/zh-hans/2.1/topics/testing/overview/