Art of Unit Test (1) - Breaking Dependency
#!/usr/bin/env python
# encoding: utf-8 import unittest """
the simplyest way to test return value
No needs to use stub
"""
class LogAnalyzer_0(object):
def IsValidLogFileName(self, fileName):
return str(fileName).endswith('.sln')
"""
However somethimes we have to rely on the extenal class or method
that we cannot control on it or it has not been finished yet
This is when we need stub to help us. eg, draw_from_weighted_range() and randrange(), interacting with filesystem.
Fakes here include stub (assert on CUT) and mock (assert on Fake) we talk about stub and mock in later posts.
Say our IsValidLogFileName() method needs read through the config file and return true if extension is supported in config file, There two big types to inject fakes into MUT(Method Under Test):
1.Test are performed on the MUT itself (eg. assert(mut.dosomething(),true)
1.1 Abstracting concrete objects into interfaces or delegates
How: Extract an interface to allow replacing or extending underlying impl
1.2 Refactoring to allow injection of faked implementations of those delegates or interface.
How:
1.2.1.Inject stub in code under test using factory design (layer of indirection 2 faking a member in factory class)
the difference is that the object initiating the stub request is the code under test.
the fake instances was set by code external to the code under test before the test started in the below.
A test configures the factory class to re turn a stub object. The class usess the factory class to get the
stub instance, which in production code would return an object that is not a stub
Preferred to using this layer
1.2.2 Injection of a stub in test code (layer of indiretion 1 faking a member in class under test)
1.2.2.1 Inject stub via ctor (cumbersome whenyou have many dependencies)
1.2.2.2 Inject stub via setter/getter
This is much simpler than ctor injection as each test can set only the dependencies
that it needs to get the test underway;
Use this when you want to signify that the dependency is optional or the dependency has
a default instance created that does not create any problems; 1.2.4.Inject stub impl via parameter
2.Test are performed on the class that inhetites from MUT (eg. assert(mut_child.dosomething(),true)
It is also known as Extract and override, which is is good to for sumulating inputs into your code under test
(in other words, return values from dependency). but it is cumbersome when you want t verify and check interactions
that are coming out of the code under test int othe dependency (in other words, it is good to play stub but very bad to play mock) 2.1 use local virtual factory method to get instance of stub
The time not to use this is there is an interface ready to fake or there is already a place that seam can be injected.
2.2 use extract and override to return a logical result instead of calling an actual denpendency
This uses a simple faked result instead of a stub
Much easier than 2.1 preferred to use
""" # Refered to "1.1 Abstracting concrete objects into interfaces or delegates"
class ExtensionMgr_AbstractedInterface(object):
def IsValid(self, filename): # should be overwriten by child
pass class FileExtensionMgr_ConcreteImpl(ExtensionMgr_AbstractedInterface):
def IsValid(self, filename):
return str(filename).endswith('.sln') # Stubs
class ExtendMgrStub(ExtensionMgr_AbstractedInterface):
def __init__(self):
self.mWillBeValid = False
return ExtensionMgr_AbstractedInterface.__init__(self) def IsValid(self, filename):
return self.mWillBeValid class ExtendMgrStub_WithoutIngeritingFrom_ExtensionMgr_AbstractedInterface(object):
def __init__(self):
self.mWillBeValid = False def IsValid(self, filename):
return self.mWillBeValid # Refered to 1.2.2.1 Inject stub impl via ctor (cumbersome whenyou have many dependencies)
class LogAnalyzer_StubInjectedViaCtor(object):
def __init__(self, iExtensionMgr):
self.mIExtensionMgr = iExtensionMgr def IsValidLogFileName(self, fileName):
self.mIExtensionMgr.IsValid(fileName) # Refered to "1.2.2.2 Inject stub impl via a setter and ggeter"
class LogAnalyzer_StubInjectedViaPropertySetter(object):
def __init__(self):
self.mIExtensionMgr = FileExtensionMgr_ConcreteImpl() def IsValidLogFileName(self, fileName):
self.mIExtensionMgr.IsValid(fileName) def SetIExtensionMgr(self, ext):
self.mIExtensionMgr = ext def GetIExtensionMgr(self):
return self.mIExtensionMgr # Refered to "1.2.1.Inject stub in code under test using factory design"
class ExtensionMgrFactory(object):
iExtMgr = None @staticmethod
def Create():
# define factory that can use and return custom manager instance
if ExtensionMgrFactory.iExtMgr is None:
ExtensionMgrFactory.iExtMgr = FileExtensionMgr_ConcreteImpl()
else:
return ExtensionMgrFactory.iExtMgr @staticmethod
def SetExtMgr(extmgr):
ExtensionMgrFactory.iExtMgr = extmgr class LogAnalyzer_StubInjectedViaFactory(object):
def __init__(self):
self.mIExtensionMgr = ExtensionMgrFactory.Create() def IsValidLogFileName(self, fileName):
self.mIExtensionMgr.IsValid(fileName) #Referred to "2.1 use local virtual factory method to get instance of stub"
class LogAnalyzer_StubInjectedViaLocalFactoryMethod(object):
def IsValidLogFileName(self, fileName):
self.GetMgr().IsValid(fileName)
def GetMgr(self):
return FileExtensionMgr_ConcreteImpl() class TestableLogAnalyzer_ReturnStub(LogAnalyzer_StubInjectedViaLocalFactoryMethod):
def __init__(self, iExtensionMgr):
self.mIExtensionMgr = iExtensionMgr
def GetMgr(self):
return self.mIExtensionMgr #Referred to "2.2 use extract and override to return a logical result instead of calling an actual denpendency"
class LogAnalyzer_OverrideMethodReturnsResult(object):
def __init__(self):
self.mIExtensionMgr = FileExtensionMgr_ConcreteImpl()
def IsValidLogFileName(self, fileName):
self.IsValidExtension(fileName)
def IsValidExtension(self,filename):
return self.mIExtensionMgr.IsValid(filename) class TestableLogAnalyzer_OverrideMethodReturnsResult(LogAnalyzer_OverrideMethodReturnsResult):
def __init__(self, is_valid_entension):
self.is_valid_entension = is_valid_entension
def IsValidExtension(self,filename):
return self.is_valid_entension # cut means by class under test mut means by method under test
class LogAnalyzerTestCase(unittest.TestCase): # No stub used just simply perform the test
def test_IsValidLogFileName_BadExtension_ReturnFalse_NoStub(self):
logAnalyzer0 = LogAnalyzer_0()
ret = logAnalyzer0.IsValidLogFileName('fn1.sl')
self.assertFalse(ret) # StubIjectedViaCtor
def test_IsValidLogFileName_BadExtension_ReturnFalse_StubIjectedViaCtor(self):
ext = ExtendMgrStub()
ext.mWillBeValid = False
logAnalyzer = LogAnalyzer_StubInjectedViaCtor(ext)
ret = logAnalyzer.IsValidLogFileName('fn1.sl')
self.assertFalse(ret) # StubIjectedViaCtor
# This is what I wrote because python is weak-type language
# so it can still work without using inheratance
def test_IsValidLogFileName_BadExtension_ReturnFalse_StubIjectedViaCtor_WithoutInhertingFrom_ExtensionMgr_AbstractedInterface(self):
ext = ExtendMgrStub_WithoutIngeritingFrom_ExtensionMgr_AbstractedInterface()
ext.mWillBeValid = False logAnalyzer = LogAnalyzer_StubInjectedViaCtor(ext)
ret = logAnalyzer.IsValidLogFileName('fn1.sl') self.assertFalse(ret) # StubInjectedViaPropertySetter
def test_IsValidLogFileName_BadExtension_ReturnFalse_StubInjectedViaPropertySetter(self):
ext = ExtendMgrStub()
ext.mWillBeValid = False logAnalyzer = LogAnalyzer_StubInjectedViaPropertySetter()
logAnalyzer.SetIExtensionMgr(ext)
ret = logAnalyzer.IsValidLogFileName('fn1.sl') self.assertFalse(ret) # StubIjectedViaFactory
def test_IsValidLogFileName_BadExtension_ReturnFalse_4_StubIjectedViaFactory(self):
ext = ExtendMgrStub()
ext.mWillBeValid = False
ExtensionMgrFactory.SetExtMgr(ext) logAnalyzer = LogAnalyzer_StubInjectedViaFactory()
ret = logAnalyzer.IsValidLogFileName('fn1.sl') self.assertFalse(ret) # OverrideMethodReturnsResult
def test_IsValidLogFileName_BadExtension_ReturnFalse_4_OverrideMethodReturnsResult(self):
is_valid_extension = False testableLogAnalyzer = TestableLogAnalyzer_OverrideMethodReturnsResult(is_valid_extension)
ret = testableLogAnalyzer.IsValidLogFileName('fnl.sl') self.assertFalse(ret) if __name__ == '__main__':
unittest.main()
Art of Unit Test (1) - Breaking Dependency的更多相关文章
- The Art of Unit Testing With Examples in .NET
The Art of Unit Testing With Examples in .NET
- C# Note37: Writing unit tests with use of mocking
前言 What's mocking and its benefits Mocking is an integral part of unit testing. Although you can run ...
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- What's the difference between a stub and mock?
I believe the biggest distinction is that a stub you have already written with predetermined behavio ...
- Adaptive Code Via C#读书笔记
原书链接: http://www.amazon.com/Adaptive-Code-via-principles-Developer-ebook/dp/B00OCLLYTY/ref=dp_kinw_s ...
- Nikola的5项依赖注入法则
本篇文章来自对 Nikola Malovic 博客文章 <Inversion Of Control, Single Responsibility Principle and Nikola’s l ...
- (C#)程序员必读的一些书籍
前言 ·貌似公司里很著名的一句话,在这里套用过来了,WP研发工程师,首先是WPF/SL研发工程师,WPF/SL研发工程师首先是是个C#研发工程师,C#研发工程师首先Windows研发工程师.Windo ...
- Snoop resynchronization mechanism to preserve read ordering
A processor employing a post-cache (LS2) buffer. Loads are stored into the LS2buffer after probing t ...
- Method and system for early speculative store-load bypass
In an embodiment, the present invention describes a method and apparatus for detecting RAW condition ...
随机推荐
- 安装oneproxy实现数据库的读写分离
领导就给了两台数据库,做主从,在从上搭建oneproxy插件,实现读写分离,一直就听说oneproxy,今天打算用一下 先下载最新的版本wget http://www.onexsoft.cn/soft ...
- HTML6注册表单输入日志标题
一.找到元素. var d = document.getElementById("") var d = document.getElementsByName("" ...
- [MFC]解决回车键 ESC 默认关闭窗口的一般方法
在一般情况下编写的对话框程序,用户在运行的时候,如果不注意按下了ENTER或者ESC键,程序就会立刻退出,之所以会这样,是因为按下Enter键时,Windows就会自动去找输入焦点落在了哪一个按钮上, ...
- 商品列表中显示类别名称而不是类别ID
商品表中的字段包裹商品信息和categoryid 若要在商品列表中显示出categoryname,有两种做法: 第一种做法: 拿到categoryid后再跟数据库连接一下,然后拿出categoryna ...
- C#获取Excel Sheet名称,对特殊字符、重名进行了处理
/// <summary> /// 获取指定Excel内Sheet集合 /// </summary> /// <param na ...
- NET Core开发-读取配置文件Configuration
ASP.NET Core开发-读取配置文件Configuration ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NE ...
- Oracle EBS-SQL (WIP-4):检查检查成品标准作业是否勾选"固定"标识.sql
select WE.DESCRIPTION 任务说明, ...
- Linux Syslog
http://blog.chinaunix.net/uid-677314-id-164215.html http://blog.chinaunix.net/uid-24708340-id-397132 ...
- USB 3.1 Type-C
[時報記者任珮云台北報導]微軟.英特爾.蘋果今年將新款PC介面升級至USB 3.1規格,Wintel陣營今年新款PC產品亦將全面採用USB 3.1介面,里昂證出具最新的報告指出,在新趨勢帶動下,台廠的 ...
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...