《Pragmatic Unit Testing In Java with JUnit》
                                                                  ——单元测试之道读后感

在公司实习这两周,很充实,但也很累,累是工作方式与以前不所不同,虽然基调一样,但细节方面存在很多不同,现在跟的项目很大,很重要,技术经理拟采用更规范,更严格的开发模式,所以,测试是重点之一,这两天,把Andrew Hunt David Thomas的《Pragmatic Unit Testing In Java with JUnit》看了,然后也看了JUnit的文档,把一些关键点和好的观点摘抄下来。

Testing Practices
Martin Fowler makes this easy for you. He says, "Whenever you are tempted to type something

into a print statement or a debugger expression, write it as a test instead." At first you

will find that you have to create a new fixtures all the time, and testing will seem to slow

you down a little. Soon, however, you will begin reusing your library of fixtures and new

tests will usually be as simple as adding a method to an existing TestCase subclass.

You can always write more tests. However, you will quickly find that only a fraction of the

tests you can imagine are actually useful. What you want is to write tests that fail even

though you think they should work, or tests that succeed even though you think they should

fail. Another way to think of it is in cost/benefit terms. You want to write tests that will

pay you back with information.

Here are a couple of the times that you will receive a reasonable return on your testing

investment:

* During Development- When you need to add new functionality to the system, write the

tests first. Then, you will be done developing when the test runs.
    * During Debugging- When someone discovers a defect in your code, first write a test

that will succeed if the code is working. Then debug until the test succeeds.

One word of caution about your tests. Once you get them running, make sure they stay

running. There is a huge difference between having your suite running and having it broken.

Ideally, you would run every test in your suite every time you change a method. Practically,

your suite will soon grow too large to run all the time. Try to optimize your setup code so

you can run all the tests. Or, at the very least, create special suites that contain all the

tests that might possibly be affected by your current development. Then, run the suite every

time you compile. And make sure you run every test at least once a day: overnight, during

lunch, during one of those long meetings….
Conclusion
This article only scratches the surface of testing. However, it focuses on a style of

testing that with a remarkably small investment will make you a faster, more productive,

more predictable, and less stressed developer.

Once you've been test infected, your attitude toward development is likely to change. Here

are some of the changes we have noticed:

There is a huge difference between tests that are all running correctly and tests that

aren't. Part of being test infected is not being able to go home if your tests aren't 100%.

If you run your suite ten or a hundred times an hour, though, you won't be able to create

enough havoc to make you late for supper.

Sometimes you just won't feel like writing tests, especially at first. Don't. However, pay

attention to how much more trouble you get into, how much more time you spend debugging, and

how much more stress you feel when you don't have tests. We have been amazed at how much

more fun programming is and how much more aggressive we are willing to be and how much less

stress we feel when we are supported by tests. The difference is dramatic enough to keep us

writing tests even when we don't feel like it.

You will be able to refactor much more aggressively once you have the tests. You won't

understand at first just how much you can do, though. Try to catch yourself saying, "Oh, I

see, I should have designed this thus and so. I can't change it now. I don't want to break

anything." When you say this, save a copy of your current code and give yourself a couple of

hours to clean up. (This part works best you can get a buddy to look over your shoulder

while you work.) Make your changes, all the while running your tests. You will be surprised

at how much ground you can cover in a couple of hours if you aren't worrying every second

about what you might be breaking.

For example, we switched from the Vector-based implementation of MoneyBag to one based on

HashTable. We were able to make the switch very quickly and confidently because we had so

many tests to rely on. If the tests all worked, we were sure we hadn't changed the answers

the system produced at all.

You will want to get the rest of your team writing tests. The best way we have found to

spread the test infection is through direct contact. The next time someone asks you for help

debugging, get them to talk about the problem in terms of a fixture and expected results.

Then say, "I'd like to write down what you just told me in a form we can use." Have them

watch while you write one little test. Run it. Fix it. Write another. Pretty soon they will

be writing their own.

So- give JUnit a try. If you make it better, please send us the changes so we can spread

them around. Our next article will double click on the JUnit framework itself. We will show

you how it is constructed, and talk a little about our philosophy of framework development.

We would like to thank Martin Fowler, as good a programmer as any analyst can ever hope to

be, for his helpful comments in spite of being subjected to early versions of JUnit.

============================================================================================
Properties of Good Tests
好的测试所具有的品质
A-TRIP

  • 自动化(Automatic)
  • 彻底的(Thorough)
  • 可重复(Repeatable)
  • 独立的(Independent)
  • 专业的(Professional)</li>

战斗口号“不要修修补补,完全重写”

============================================================================================

测试的礼貌

  • 不完整的代码
  • 不能便宜的代码
  • 代码能便宜,但是会破外已经存在的代码,比如使得已经存在的代码编译失败
  • 没有相应单元测试的代码
  • 不能通过单元测试的代码
  • 通过了自己的测试,但是导致系统其他地方的其他测试失败的代码</li>

============================================================================================
编码和评审以这样的顺序进行:
1、编写 test case 和/或测试代码
2、评审 test case 和/或测试代码
3、经评审修改 test case 和/或测试代码
4、编写能通过所有测试的产品代码
5、评审产品代码和测试代码
6、在每次评审后,修改测试代码和产品代码
============================================================================================
CORRECT边界条件

  • 一致性(conformance)——值是否符合预期的格式?
  • 有序性(Ordering)——一组值是该有序的,还是该无序的?
  • 区间性(Range)——值是否在一个合理的最大值和最小值的范围之内?
  • 引用,耦合性(Reference)——代码是否引用了一些不受代码本身直接控制的外部因素?
  • 存在性(Existence)——值是否存在(例如,非null, 非零, 包含于某个集合等)?
  • 基数性(Cardinality)——是否恰好有足够的值?
  • 时间性,绝对的或者相对的(Time)——所有事情是否都是按顺序发生的?是否在正确的时间?是否及时?

转载于:https://www.cnblogs.com/TV9/archive/2006/03/31/363954.html

《Pragmatic Unit Testing In Java with JUnit》—单元测试之道读后感相关推荐

  1. 《Pragmatic unit testing:in java with Junit》阅读

    SQ3R阅读法: Survey:阅读之前的浏览,查阅   1.书名:<Pragmatic unit testing:in java with Junit>,中文译名<单元测试之道Ja ...

  2. 艾伟_转载:单元测试之道(使用NUnit)

    首先来看下面几个场景你是否熟悉 1.你正在开发一个系统,你不断地编码-编译-调试-编码-编译-调试--终于,你负责的功能模块从上到下全部完成且编译通过!你长出一口气,怀着激动而又忐忑的心情点击界面上的 ...

  3. 补习系列(8)-springboot 单元测试之道

    目录 目标 一.About 单元测试 二.About Junit 三.SpringBoot-单元测试 项目依赖 测试样例 四.Mock测试 五.最后 目标 了解 单元测试的背景 了解如何 利用 spr ...

  4. 单元测试之道——基于junit的java单元测试 的读书笔记与思考

    第二章 你的首个单元测试 这一张已经忘了差不多,如何去测试,其实首先还是得先去考虑怎么去测试,这个就需要看代码单元的需求了. 一个简单的例子.测试排序 第二章 你的首个单元测试 这一张已经忘了差不多, ...

  5. 电子书下载:Pragmatic Unit Testing in C# with NUnit

    下载:http://www.ctdisk.com/file/9294087 转载于:https://www.cnblogs.com/MaxWoods/archive/2012/09/05/267123 ...

  6. (4.5.4)Android測试TestCase单元(Unit test)測试和instrumentationCase单元測试

    Android单元和instrumentation单元測试 Developing Android unit and instrumentation tests Android的单元測试是基于JUnit ...

  7. Android单元测试之Local unit tests(上)

    Android单元测试之Local unit tests(上) 简介 本地单元测试 JUnit 4 添加依赖 测试例子 结论 Mockito 添加依赖 测试例子-mock基本使用 测试例子-mock与 ...

  8. Java单元测试之模拟利器-使用PowerMock进行Mock测试

    首页 国产Linux Linux命令 openSUSE ArchLinux Slackware FreeBSD Ubuntu CentOS Fedora Debian PHP教程 在线教程 登录 注册 ...

  9. 「BUAA OO Unit 3 HW12」第三单元总结 —— JML规格化设计与基于社交网络背景的图论算法

    「BUAA OO Unit 3 HW12」第三单元总结 目录 Part0 前言 Part1 测试分析 1.1 黑箱白箱 1.2 多种测试思路分析 1.2.1 单元测试 1.2.2 功能测试 1.2.3 ...

最新文章

  1. 阿里P7背调红灯:被前前公司说坏话,修改领导名被查
  2. 关于jspsmartupload中的各种问题
  3. 一个普通80后的IT Pro去溜冰的感慨
  4. 2020最新Java面试题(java,JavaWeb,数据库,框架),常见面试题及答案汇总
  5. POJ - 3255 Roadblocks(次短路)
  6. java中什么是释放已经持有的锁_java多线程什么时候释放锁
  7. 解析全球热点安全事件背后的玄机
  8. eplan图纸怎么发给别人_手机拍的照片怎么打包发给别人
  9. GO语言学习之路22
  10. 如何使用QQ快速截屏
  11. c语言专家编程,读书笔记
  12. 使用segue进行页面跳转
  13. 分享一些网站有免费的pdf书籍
  14. 系统中如何做到实名认证
  15. anker2017拆_Anker的Eufy Genie与Amazon Echo Dot:值得吗?
  16. 【《Redis深度历险》读书笔记(1)】基础:万丈高楼平地起 ——Redis 5种基础数据结构
  17. 在冰山一角之下:情绪分析研究的当前挑战和新方向。
  18. 北京市工作居住证只有电子版没有纸质版
  19. 如何快速入门深度学习写论文?
  20. Python 常用的标准库以及第三方库

热门文章

  1. 详解Linux系统Vi 和 Vim中正常模式、编辑模式、命令模式相互转化,以及vim命令使用
  2. 苹果iPad2 3G版和WiFi版的区别!
  3. Free Download Manager 6.13.3中文版-mac必备fdm下载工具
  4. phalcon_Phalcon 2.0 Alpha着陆
  5. Flume四:多路复用(ChannelSelector之Multiplexing)+自定义拦截器
  6. 以 Mapbox Terrian-RGB 模型发布高程数据
  7. 使用FreeMarker给word文档里生成柱状图
  8. 数据结构到底是个什么玩意儿?——数据结构总结篇
  9. 重读《C primer plus》(七)
  10. OR-Tools求解仓库选址和钢材取料问题