Unit Tests
The Three Laws of TDD
First Law : you may not write production code until you have written a failing unit test.
Second Law : you may not write more of a unit test than is sufficient to fail, and not compiling is failing.
Third Law : you may not write more production code than is sufficient to pass the currently failing test.
These three laws lock you into a cycle that is perhaps thirty seconds long.The tests and the production code are written together,with the tests just a few seconds ahead of the production code.
Keeping Tests Clean
Dirty tests is equivalent to, if not worse than, having no tests.The problem is that tests must change as the production code evolves.The dirtier the tests, the harder they are to change.The more tangled the test code, the more likely it is that you will spend more time cramming new tests into the suite than it takes to write the new production code.As you modify the production code, old tests start to fail, and the mess in the test code makes it hard to get those tests to pass again.So the tests become viewed as an ever-increasing liability.
Test code is just as important as production code.Its is not a second-class citizen.It requires thought, design and care.It must be kept as clean as production code.
Tests Enable the -ilities
Unit tests keep our code flexible, maintainable and reusable.Having an automated suite of unit tests that cover the production code is the key to keeping your design and architecture as clean as possible.Test enable all the -ilities, because tests enable change.
Clean Tests
Readability is perhaps even more important in unit tests than it is in production code.What makes tests readable?The same thing that makes all code readable: clarity, simplicity and density of expression.In a test you want to say a lot with as few expressions as possible.
The tests get right to the point and use only the data types and functions that they truly need. Anyone who reads these tests should be able to work out what they do very quickly, without being misled or overwhelmed by details.
Domain-Specific Testing Language
Rather than using the APIs that programmers use to manipulate the system, we build up a set of functions and utilities that make use of those AOIs and that make the tests more convenient to write and easier to read.These functions and utilities become a specialized API used by the tests.They are a testing language that programmers use to help themselves to write their tests and to help those who must read those tests later on.
A Dual Standard
The code within the testing API does have a different set of engineering standards than production code.It must still be simple, succinct and expressive,but it need not be as efficient as production code.
There are things that you might never do in a production environment that are perfectly fine in a test environment.Ususally they involve issues of memory or CPU efficiency.But they never involve issues of cleanliness.
One Assert per Test
There is a school of thought that says that every test function in a Junit test should have one and only one assert statement.This rule may seem draconian, but the advantage can be seen that those tests come to a single conclusion that is quick and easy to understand.
Notice that I have changed the names of the functions to use the common given-when-then convention.This makes the tests even easier to read.Unfortunately, splitting the tests as shown results in a lot of duplicate code.
We can eliminate the duplication by using the TEMPLATE METHOD pattern and putting the given/when parts in the base class, and then parts in different derivatives.Or we could create a completely separate test class and put the given and wehn parts in the @Before functions, and the when parts in each @Test function.But this seems like too much mechanism for such a minor issue.
Single assert rule is a good guideline.I usually try to create a domain-specific testing language that supports it.But I am not afraid to put more than one assert in a test.I think the best thing we can say is that the number of asserts in a test ought to be minimized.
Single Concept per Test
Perhaps a better rule is that we want to test a single concept in each test function.We don't want long test functions that go testing one miscellaneous.
F.I.R.S.T
Clean tests follow five other rules that form the above acronym:
Fast -- Tests should be fase.They should run quickly.
Independent -- Tests should not depend on each other.One test should not set up the conditions for the next test.You should be able to run each test independently and run the tests in any order you like.When test depend on each other, then the first one to fail causes a cascade of downstream failures, making diagnosis difficult and hiding downstream defects.
Repeatable -- Tests should be repeatable in any environment.You should be able to run the tests in the production environment, in the QA environment, and on your laptop while riding home on the train without a network.
Self-Validating -- The tests should have a boolean output.Either they pass or fail.You should not have to read through a log file to tell whether the tests pass.You should not have to manually compare two different text files to see whether the tests pass.If the tests aren't self-validating, then failure can become subjective and running the tests can require a long manual evaluation.
Timely -- The tests need to be written in a timely fashion.Unit tests should be written just before the production code that makes them pass.If you write test after the producion code,then you may find the production code to be hard to test.You may decide that some production code is too hard to test.You may not design the production code to be testable.
Conclusion
We have barely scratched the surface of this topic.Indeed, I think an entire book could be written abut clean tests.Tests are as important to the health of a project as the production code is.Perhaps they are even more important,because tests preserve and enchance the flexibility, maintainability, and reusability of the production code.
Unit Tests的更多相关文章
- [转]Creating Unit Tests for ASP.NET MVC Applications (C#)
本文转自:http://www.asp.net/mvc/tutorials/older-versions/unit-testing/creating-unit-tests-for-asp-net-mv ...
- Building Local Unit Tests
If your unit test has no dependencies or only has simple dependencies on Android, you should run you ...
- 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 ...
- [Java in NetBeans] Lesson 07. JavaDoc and Unit Tests
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. organize code into packages Create a package if want to make th ...
- [Python Test] Use pytest fixtures to reduce duplicated code across unit tests
In this lesson, you will learn how to implement pytest fixtures. Many unit tests have the same resou ...
- Android测试:Building Local Unit Tests
原文:https://developer.android.com/training/testing/unit-testing/local-unit-tests.html 如果你的单元测试没有依赖或者只 ...
- [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests
In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...
- Unit Tests Tool - <What is “Mock You”> The introduction to moq #Reprinted#
From: http://www.cnblogs.com/wJiang/archive/2010/02/21/1670632.html Moq即Mock You Framework,故名思意是一个类似 ...
- ASP.Net MVC3 - The easier to run Unit Tests by moq #Reprinted#
From: http://www.cnblogs.com/techborther/archive/2012/01/10/2317998.html 前几天调查完了unity.现在给我的任务是让我调查Mo ...
随机推荐
- mac攻略(一) -- git使用
1.首先在官网下载 https://git-scm.com/download/mac 2.然后安装git(mac自带git) 3设置Git的user name和email: $ git c ...
- Android知识散点
1.所有活动都需要在AndroidMainfest.xml中注册后才能生效. <activity android:name=".MainActivity" android:l ...
- EasyUI关于 numberbox,combobox,validatebox 的几个小问题
在最近的项目中,首次使用到了 网页的一个布局框架——EasyUI,感觉这个框架特别牛,兼容性很不错,页面效果也挺不错,可是在使用标题上三个控件过程中遇到几个很奇特的问题,让我头疼不已,所以在此给广大I ...
- C#定时执行一个操作
一个客户端向服务器端socket发送报文,但是服务器端限制了发送频率,假如10秒内只能发送1次,这时客户端也要相应的做限制,初步的想法是在配置文件中保存上次最后发送的时间,当前发送时和这个上次最后时间 ...
- Ubuntu 16.04 + Caffe
主要参考: https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide http://caffe.berke ...
- iOS - Mac Apache WebServer 服务器配置
前言 Apache 是目前使用最广的 Web 服务器,可以支持各种脚本的执行. Mac 系统自带,无需单独安装,只需要修改几个配置就可以,简单,快捷. 有些特殊的服务器功能,Apache 都能很好的支 ...
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...
- iOS开发 沙盒路径和使用
1.模拟器沙盒目录文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library.因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件: ...
- php 对多维数组排序array_multisort
php 对多维数组排序array_multisort 排序顺序标志: SORT_ASC - 按照上升顺序排序 SORT_DESC - 按照下降顺序排序 排序类型标志: SORT_REGULAR - 将 ...
- DOS批处理不支持将UNC 路径作为当前目录的巧妙解决方案
DOS批处理不支持将UNC 路径作为当前目录的巧妙解决方案在有些时候,需要在批处理中操作UNC目录,但批处理并不能直接对UNC目录进行操作,怎么办? 废话少说,直接上代码,打开网上邻居→整个网络→Mi ...