[Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears - ISerializable - Roy Osherove's Blog

[Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

My latest MSDN Magazine article titled
is out in the online January Issue here.
 
It's based on real issues, guidelines I've developed and questions I've encountered during the last few years teaching and mentoring teams in test driven and agile development, and contains general rules I think no person writing unit tests should go without. It took blood sweat and tears to make this one, and I hope you like it (though the editor seems to have thoroughly cut down large pieces of it including changing all my samples to be VB.NET instead of C#.. especially the part that says that this article is targeted at developers using any version of a unit test framework out there.. grr!)
 
Anyways, here's the summary:
 
"There's a lot of talk these days about unit testing and how one should go about writing unit tests for their applications under different scenarios (for starters, see my June 2005 MSDN®Magazine article on testing your data layer, available at Know Thy Code: Simplify Data Layer Unit Testing using Enterprise Services). That means there are a lot of developers who say to themselves (and to their teams) "Hey, we should start writing tests, too!" And so they begin writing unit test upon unit test until they reach a point where the tests themselves become a problem. Perhaps maintaining them is too hard and takes too long, or they are not readable enough to make sense, or maybe they have bugs.

It is at that point that developers are forced to make a tough decision: dedicate precious time to improving their tests or ignore the problem, effectively throwing away their hard work. The cause of this problem is simply inexperience writing unit tests.

In this article, I'll try to bring you some of the most important practices I've learned over the years while developing and consulting, and while training developers. These tips should help you write effective, maintainable, and robust unit tests. And I hope this advice helps you to avoid huge amounts of wasted time and effort."

Published Tuesday, December 13, 2005 6:54 PM by RoyOsherove

Comments

Tuesday, December 13, 2005 1:11 PM by Joshua Flanagan

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

I've been meaning to blog about this article, to recommend it to everyone starting out with TDD (but since you get a lot more traffic than I do, why not write it here will people will actually see it?).

You really did a fine job of capturing the common gotchas, and giving good guidance for how to approach writing your tests. A lot of other sources tell you "what" and "why", but its tough to find a good source on "how". This is it. Thanks a lot!
Tuesday, December 13, 2005 1:54 PM by Eber Irigoyen

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

I was expecting something more than a test for a sum, there are many people that talk about TDD, but very few show actual code for more complicated scenarios, specially when using data (databases, xml, whatever files, etc), or how you separate the test code from the production code, etc
Tuesday, December 13, 2005 3:41 PM by SBC

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

congratulations Roy! It's definitely on my reading list in the near coming days..
Wednesday, December 14, 2005 3:20 AM by Dan Bunea

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

Hi,

Test code organization can be a problem just as big as the code organization can be and poiting out rules that apply to test code organization to make it bug free, useful, self explaining and maintanable is a great thing you did.

There is also another great resource about test code organization, patterns and smells at: www.testautomationpatterns.com

Thanks,
Dan Bunea
http://danbunea.blogspot.com
Wednesday, December 14, 2005 3:57 AM by Roy Osherove

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

Eber: I was expressly using a simple example, since the rules aply to tests of any level of complexity. If you'd like data related examples, I have an article specifically about that, linked to my this article.
http://msdn.microsoft.com/msdnmag/issues/05/06/UnitTesting/

Can you elaborate what kind of guidelines you'd like to see that are not covered?
I'd love to see what I should be concentrating on.
Wednesday, December 14, 2005 3:04 PM by Nikolai

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

Roy you mention in your article that you should "Avoid Dependencies Between Tests". I sometimes call one test from another to ensure that data/state is setup for this test to run. It does require that the other test is run before hand, but I guess it does create a dependency between the two.

What do you think is a better alternative to this? Like a database setup and tear down scripts for the test suite? Or to use the setup teardown sections of the test to seutp this data?
Wednesday, December 14, 2005 4:55 PM by Roy Osherove

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

Nikolai: You're calling the other test because you'd like to share the same setup between the tests, but you're not only creating a dependency, you're also essentially running two tests (two asserts will take place).
Instead, refactor the common setup code in the original test you're running (which you are calling from the 2nd test) into a utility method which is in the same class. Both tests will call that method when they begin, but that way each test is free of dependencies.
If all the tests in the class need this common code feel free to put that share setup call in the [setup] method.

If it's a DB setup code you may want to either use setup scripts, or you can use the enterprise services rollback method I describe in my other article relating to DB unit testing :
http://msdn.microsoft.com/msdnmag/issues/05/06/UnitTesting/

Hope this helps,
Roy.
Thursday, December 15, 2005 3:39 PM by Nikolai

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

Great thanks for the tips Roy
Friday, December 16, 2005 7:38 AM by Rui Quintino

# re: [Article] Unit Testing Tips - Write Maintainable Unit Tests That Will Save You Time And Tears

Great article Roy (as usual) :)

One thing I was thinking about, on multiple asserts. I mostly agree with the single assert per test, but sometimes I feel that we're just not using multiple asserts because the framework simply doesn't support that, and no other "best practice" reason.

Why not supporting code like:

AssertGroup agroup= new AssertGroup();

agroup.add(new AssertEqual(...));
agroup.add(new AssertNotNull(...));
....
agroup.AssertAll()

Could this reduce some test code? Probably. Is it a good ideia? Don't know yet.

Another possible conclusion is that, currently, the feedback we get from tests is very limited (to a failed single assert).

Anyway, just some lost thoughts. :)

RQ