标签:def else contain split providing 轻量级 some 关系 val
测试过程中经常伴随着某种外部依赖,列举如下:
大多数实际的测试需要各种形式的数据,通常情况下,数据从数据库中读取。下面是一些控制数据源依赖关系的技术
为控制系统之外资源的行为,可以虚拟化它们,即构造另一个版本的资源,新的版本可以模拟原有资源的API,而不需要内部实现。有如下技术:
为测试期间的函数调用提供标准相应
仿制并代替实际对象的API
可以提供一个非常轻量级的实现
本文先讲解仿制技术,其他方式后续进行补充!
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()
标签:def else contain split providing 轻量级 some 关系 val
原文地址:https://www.cnblogs.com/hester/p/12121617.html