罗耀秋,字无介,号馨园主人。湖南浏阳人。好旅游、音乐、垂钓、美食、足球。 微薄:http://weibo.com/luoyaoqiu 微信:luoyaoqiu 邮件:luoyear@163.com

A Set of Unit Testing Rules

上一篇 / 下一篇  2006-12-06 09:16:33 / 个人分类:单元测试

by Michael Feathers
September 9, 2005

Summary
Don't let slow tests bog you down.


Teams that adopt agile practices often adopt Test Driven Development (TDD), which means, of course, that they end up writing a lot of tests. In general, that’s great but there is a failure case for teams that attempt to get test infected; you can end up writing very slow tests that take so long to run that they essentially start to feel like baggage even though they help you catch errors.

This issue with unit tests isn’t a new issue, it’s been around for a while, and there are a couple of ways of handling it. In Extreme Programming, the typical way of handling it is to periodically go back and optimize your tests as they get too slow. In many cases this works well, but the amount of optimization that you have to do can be rather large if you haven’t been conscious of how long your tests run during development. In one case that stands out in my memory, I visited a team on the east coast about four years ago that wrote oodles of tests against their EJB environment. The tests hit a server and went through session beans, entity beans, down to the bowels of the database and then up again. Their refrain? “We don’t like writing unit tests any more; they take too long to run.” I didn’t blame them for feeling that way, but I also didn’t agree that they had written any unit tests.

The problem is rather common. I’ve spoken to other XPers about it over the years and I sort of figured that the way that I handled it was common, but I was surprised to discover (on the XP yahoo group this week) that it was also a bit contentious. Here’s what I typically say when I run into teams that have this problem.

A test is not a unit test if:

· It talks to the database

· It communicates across the network

· It touches the file system

· It can't run at the same time as any of your other unit tests

· You have to do special things to your environment (such as editing config files) to run it.

Tests that do these things aren't bad. Often they are worth writing, and they can be written in a unit test harness. However, it is important to be able to separate them from true unit tests so that we can keep a set of tests that we can run fast whenever we make our changes.

That might sound a little severe, but it is medicine for a common problem. Generally, unit tests are supposed to be small, they test a method or the interaction of a couple of methods. When you pull the database, sockets, or file system access into your unit tests, they aren’t really about those methods any more; they are about the integration of your code with that other software. If you write code in a way which separates your logic from OS and vendor services, you not only get faster unit tests, you get a ‘binary chop’ that allows you to discover whether the problem is in your logic or in the things are you interfacing with. If all the unit tests pass but the other tests (the ones not using mocks) don’t, you are far closer to isolating the problem.

Frankly, we need both kinds of tests, but these "pure" unit tests are undervalued.


TAG: 单元测试

 

评分:0

我来说两句

luoyear

luoyear

罗耀秋,字无介,号馨园主人。湖南浏阳人。好旅游、音乐、垂钓、美食、足球。 10多年质量管理,外包管理,培训管理及招募管理经验,对敏捷,CMMI/ISO/TL9000/6Sigma/PMP/PrinceII/测试咨询等有一定的了解。 邮件:luoyear@163.com,微信:luoyaoqiu,新浪微薄:http://weibo.com/luoyaoqiu

日历

« 2024-05-03  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 203814
  • 日志数: 56
  • 图片数: 3
  • 书签数: 2
  • 建立时间: 2006-12-01
  • 更新时间: 2012-12-07

RSS订阅

Open Toolbar