Glossary

  • SUT SYSTEM UNDER TEST
  • CUT CLASS UNDER TEST
  • MUT METHOD UNDER TEST

Tests without Use of Stub

You do not have to use stub when there are no external dependencies involved in the system under test (SUT). The point is only take it into consideration when you really need it

'''CUT'''
class LogAnalyzer_NoUseStub(object):
def IsValidLogFileName(self, fileName):
return str(fileName).endswith('.sln')
''' TEST CASE'''
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)

Tests Must Use Stub

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):

  • Test are performed on the MUT itself (eg. assert(mut.dosomething(),true)
  • Step 1 - Abstracting concrete objects into interfaces or delegates

    How: Extract an interface to allow replacing or extending underlying impl
  • Step 2 - Refactoring to allow injection of faked implementations of those delegates or interface

    How:
  • 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
  • Injection of a stub in test code (layer of indiretion 1 faking a member in class under test)
  • Inject stub via ctor (cumbersome whenyou have many dependencies)
  • 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;
  • Inject stub impl via parameter
  • 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)
  • 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.
  • 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

test md的更多相关文章

  1. 好用的Markdown编辑器一览 readme.md 编辑查看

    https://github.com/pandao/editor.md https://pandao.github.io/editor.md/examples/index.html Editor.md ...

  2. github上readme.md 格式

    参考:https://github.com/guoyunsky/Markdown-Chinese-Demo/edit/master/README.md

  3. .md文件的语法

    md全称是Macdown,.md文件可以当记事本一样使用,作为编辑软件,还可以自己添加样式,图片,链接等,可以用记事本打开,也可以保持样式排版转换为html文件,语法比较简单..md除了编辑容易的优势 ...

  4. md语法之行内代码和代码片续集

    md语法之行内代码和代码片 一行之内嵌入一小段代码, 简称行内代码. 其方法为: 用撇号把代码围起来. 比如: import numpy as ny就可以了. 代码片的方法: 三个连续的撇号+pyth ...

  5. md语法之行内代码和代码片

    md语法之行内代码和代码片 比如说要在行内写上一句或者半句代码(代码的意思就是某种脚本语言), 用撇号围起来就可以了. 比如: import pandas as pd 写代码片(单独的一块脚本语言)的 ...

  6. bootstrap 之 xs,sm,md,lg && 主要颜色

    mobile – xs ( <768px ) tablet – sm ( 768~991px ) desktop – md ( 992~1170px ) large desktop – lg ( ...

  7. Markdown(MD)写作

    简洁的写作 目前正逐步使用Makedown来写博客或其它的文档,本文记录一下Markdown的相关知识 Markdown语法 标题 # 内容 (一级标题) ## 内容 (二级标题) ### 内容 (三 ...

  8. /MT、/MD编译选项,以及可能引起在不同堆中申请、释放内存的问题

    一.MD(d).MT(d)编译选项的区别 1.编译选项的位置 以VS2005为例,这样子打开: 1)         打开项目的Property Pages对话框 2)         点击左侧C/C ...

  9. 警告 - no rule to process file 'WRP_CollectionView/README.md' of type net.daringfireball.markdown for architecture i386

    warning: no rule to process file '/Users/mac/Downloads/Demo/Self/WRP_CollectionView/WRP_CollectionVi ...

  10. Markdown语言.md文件

    转自:http://www.kuqin.com/shuoit/20141125/343459.html 之前一直在使用github,也在上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是 ...

随机推荐

  1. jQuery load()和ready()

    ready与load谁先执行: 大家在面试的过程中,经常会被问到一个问题:ready与load那一个先执行,那一个后执行?答案是ready先执行,load后执行. DOM文档加载的步骤: 要想理解为什 ...

  2. Objective-c开发教程--MRC和ARC混编

    iOS5.0以后就开始可以使用ARC来代替之前的MRC. 1.ARC中使用MRC的类.方法如下: 在targets的build phases选项下Compile Sources下选择要不使用arc编译 ...

  3. QF——iOS中数据持久化的几种方式

    数据持久化的几种方式: 一.属性列表文件: .plist文件是种XML文件.数组,字典都可以和它互相转换.数组和字典可以写入本地变成plist文件.也可以读取本地plist文件,生成数组或字典. 读取 ...

  4. Python进阶之map()、reduce()、filter()

    map()函数 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...

  5. PHP设计模式之单例模式(数据库访问)

    1.什么是单例模式? 作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例.它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用. 2.单例模式的 ...

  6. JavaWeb基础之tomcat部署

    有些东西,看起来简单,做起来也简单,简单的你并不一定知道! 问题:如何将一个java project转变为 web project,并使其自动部署到tomcat? 1.先建立一个Java Projec ...

  7. Unicode字符列表

    注:除非有特别指明,否则以下符号皆属“半角”而非“全角”. 代码 显示 描述 U+0020  空格 U+0021 ! 叹号 U+0022 " 双引号 U+0023 # 井号 U+0024 $ ...

  8. Kotlin Vertx

    Kotlin & Vertx Kotlin 是一门好语言,值得大家了解一下. Vertx 是一个好框架,也值得大家了解一下. Kotlin 写过js,也写过一点点go,主力一直是java.用了 ...

  9. jQuery中find()和filter()的区别

    filter(expr):筛选出与指定表达式匹配的元素的元素集合,其中expr可以是多个选择器的组合.是对自身集合元素的筛选. find()会在元素内寻找匹配元素,而filter()是筛选元素:是对它 ...

  10. RedHat Enterprise Linux 6.3 安装Oracle Database 11g

    按照以下文章正确将oracle安装在linux上 http://yiyiboy2010.iteye.com/blog/1670795 http://mirrors.163.com/centos/6.5 ...