unittest中控制并孤立各种外部依赖

一、外部依赖

测试过程中经常伴随着某种外部依赖,列举如下:

  1. 可能需要从数据库加载、保存数据
  2. 可能依赖于某个特定的时间
  3. 可能从web上获取数据

二、隔离方式

2.1 数据源

大多数实际的测试需要各种形式的数据,通常情况下,数据从数据库中读取。下面是一些控制数据源依赖关系的技术

  • 使用本地文件代替数据库
  • 使用内存中的数据库
  • 使用一个测试数据库

2.2 资源虚拟化

为控制系统之外资源的行为,可以虚拟化它们,即构造另一个版本的资源,新的版本可以模拟原有资源的API,而不需要内部实现。有如下技术:

  • 存根(stub)

   为测试期间的函数调用提供标准相应

  • 仿制(mock)

     仿制并代替实际对象的API

  • 伪造(fake)

   可以提供一个非常轻量级的实现

本文先讲解仿制技术,其他方式后续进行补充!

datetimehelper.py代码如下:

# Code Listing #3

""" Module datetime helper - Contains the class DateTimeHelper providing
some helpful methods for working with date and datetime objects """

import datetime


class DateTimeHelper(object):
    """ A class which provides some convenient date/time
    conversion and utility methods """

    def today(self):
        """ Return today's datetime """
        return datetime.datetime.now()

    def date(self):
        """ Return today's date in the form of DD/MM/YYYY """
        return self.today().strftime("%d/%m/%Y")

    def weekday(self):
        """ Return the full week day for today """
        return self.today().strftime("%A")

    def us_to_indian(self, date):
        """ Convert a U.S style date i.e mm/dd/yy to Indian style i.e dd/mm/yyyy """

        # Split it
        mm, dd, yy = date.split('/')
        yy = int(yy)
        # Check if year is >16, else add 2000 to it
        if yy <= 16:
            yy += 2000
        # Create a date object from it
        date_obj = datetime.date(year=yy, month=int(mm), day=int(dd))
        # Return it in correct format
        return date_obj.strftime("%d/%m/%Y")

测试代码如下:

# Code Listing #5

""" Module test_datetimehelper -  Unit test module for testing datetimehelper module """

# Note - This is the second version of test_datetimehelper module so named as test_datetimehelper2.py

import unittest
import datetime
import datetimehelper

from unittest.mock import patch


class DateTimeHelperTestCase(unittest.TestCase):
    """ Unit-test testcase class for DateTimeHelper class """
    
    def setUp(self):
        self.obj = datetimehelper.DateTimeHelper()

    def test_date(self):
        """ Test date() method """

        # Put a specific date to test
        my_date = datetime.datetime(year=2016, month=8, day=16)

        # Patch the 'today' method with a specific return value
        with patch.object(self.obj, 'today', return_value=my_date):
            response = self.obj.date()
            self.assertEqual(response, '16/08/2016')

    def test_weekday(self):
        """ Test weekday() method """

        # Put a specific date to test
        my_date = datetime.datetime(year=2016, month=8, day=21)

        # Patch the 'today' method with a specific return value
        with patch.object(self.obj, 'today', return_value=my_date):
            response = self.obj.weekday()
            self.assertEqual(response, 'Sunday')            
                          
    def test_us_india_conversion(self):
        """ Test us=>india date format conversion """

        # Test a few dates
        d1 = '08/12/16'
        d2 = '07/11/2014'
        d3 = '04/29/00'
        self.assertEqual(self.obj.us_to_indian(d1), '12/08/2016')
        self.assertEqual(self.obj.us_to_indian(d2), '11/07/2014')
        self.assertEqual(self.obj.us_to_indian(d3), '29/04/2000')


if __name__ == "__main__":
    unittest.main()

  

  

猜你喜欢

转载自www.cnblogs.com/hester/p/12121617.html