Monday, May 24, 2010

Test code quality

I've been meaning to write about this for awhile, but it always seems so contentious for some reason. I'm sure we've all seen the worst examples before. I once opened up a 5,000 line unit test of a Spring MVC controller. It got this bad because every bit of setup was cut and paste into each new test function. Of course, it should have been a sign to the original author that refactoring was needed when there was that much setup required in the first place. But it still begs the question: What level of quality should be expected of test code?

I've worked with lots of great developers that would see so much as a line of duplication and take it upon themselves to drop everything, including the donut in their hand, to remove it from code. However, when it comes to test code, that same developer would shrug their shoulders and say: 'It's only test code'. I just don't get this. If lack of reuse and duplicate code would cause an aneurysm in normal code, why is it acceptable in test code? For that matter, why would the way you treat test code be any different at all?

When we write unit tests, it's code we intend to last just as long as the class it's testing, or at least until the assumptions we made about the code have changed. And given how slowly some companies could change even those basic assumptions, that could mean never. Testcode still needs to be easy to maintain, and easy to understand.

Here's some of the reasoning I've heard:

I don't want to make the unit test complicated, it should be easy to understand the intention

If abstractions and code reuse done properly can make code easier to understand, why would the same not apply to test code? How would duplication between two unit test classes aid in the clarity of the individual test?

Unit tests should be completely isolated from each other

I'll agree to this in principle. But I think it can be taken too far. Unit tests should be able to be run side effect free. You should be able to run each individual test by itself, or with any number of other unit tests without the existence or absence of such tests causing failures. However, this has nothing to do with sharing abstractions with each other, or reusing the same setup data if more than one class under test uses the same domain objects. As long as you're creating new instances in your test classes, and not accessing them through stateful static calls, there should be no issue.

Modifying one test to pass shouldn't cause another to break

This is a tricky one that has bitten me numerous times before. If you create some kind of abstraction, such as some type of a builder to create a set of test data for multiple tests, you run the risk of breaking a lot of tests if you change something in the builder to make one pass. It can be extremely disheartening to watch the unit test you were working with pass, only to see hundreds of test failures when you run the entire build.

However, despite the pain this has caused me in the past, doesn't this seem like a normal coding problem? Maybe it's coupling/cohesion, or demeters law, or any other random law. There's probably a perfectly well known refactoring mechanism to make this test code easier to modify without side affects. There is no reason not to apply them to your unit test code as well.

Now though, there comes the dirty part, that only an 'Enterprise' developer will know the unfortunate reality of. What if your test code looks nothing like your real code, but there's nothing really wrong with it except it breaks many of the largely nonsensical 'coding standards' put in place by some type of 'governing body'? What if *gasp* its better as a result?


Dokemion said...

When you are trying to learn java you have to under go training.

dadraig said...

A strong necessity of good results makes modern students seek for a company with long term aspirations and noble recognition. Thus, in order to meet expectation of learners and to exceed them fully we suggest best online dissertation help on a regular basis for everyone. This company assures quality of its informational materials and short educational stories, so that you could feel your studies are getting better each year.

Larry Morales said...

Thanks for sharing your perspective on unit testing. The way you approach unit testing strategies are good. You have presented its technical sides perfectly. Essay writing services

Sandra Sparks said...

thanks for share a valuable information...

for Custom Essay Writing Service

Sandra Sparks said...

thanks for sharing a vulnerable information with us....


Buy Custom Essays

Denisa Cretu said...

Thanks for the great post. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. , ,

eric yao said...

Coach Outlet

Christian Louboutin Shoes

Valentino Shoes

Michael Kors Outlet

Coach Factory Outlet

Coach Outlet Online

Coach Purses

Kate Spade Outlet

Toms Shoes

Hermes Belts

Louis Vuitton

Fendi Handbags

Giuseppe Shoes

Michael Kors Outlet

Stephen Curry Shoes

Salomon Shoes

North Face Outlet

Coach Outlet

North Face Outlet

Burberry Outlet

North Face Outlet

North Face Jackets

Skechers Shoes

Toms Outlet

North Face Outlet

Nike Air Max

Nike Hoodies

Marc Jacobs Handbags

Marc Jacobs Outlet

Jimmy Choo Shoes

Jimmy Choos

Burberry Belt

Louis Vuitton Belt

Salvatore Ferragamo

Marc Jacobs Handbags

Lululemon Outlet

True Religion Outlet

Tommy Hilfiger

Michael Kors Outlet

Coach Outlet

Red Bottoms

Kevin Durant Shoes

New Balance Outlet

Adidas Outlet

Coach Outlet Online

Stephen Curry Jersey

Vans Outlet

Ralph Lauren Outlet

True Religion Outlet

ED Hardy Outlet

North Face Outlet

UGG Outlet

UGG Outlet

North Face Outlet

Ugg Boots Sale

UGGS For Women

Skechers Go Walk

Adidas Yeezy Boost

Adidas Yeezy

Adidas NMD

Coach Outlet

for IT the said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

for IT the said...

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training

Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai Java Online Training Java Online Training Java Online Training Java Online Training

for IT the said...

Java Training Institutes Java Training Institutes Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Spring Training Institutes in Chennai Spring Training Institutes in Chennai

for IT the said...

Interesting Article

Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

Java Online training From India | spring hibernate training in chennai

for IT the said...

Spring Online Training Spring Batch Training Online | Spring MVC Training in Chennai