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

My blog has moved. You can view this post at the following address: http://www.osherove.com/blog/2005/12/13/article-unit-testing-tips-write-maintainable-unit-tests-that.html
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