Showing posts with label unit testing. Show all posts
Showing posts with label unit testing. Show all posts

Sunday, August 16, 2009

Oh no, a long method... again...

A few days ago, a fellow coworker asked me to help her in fixing some "legacy unit tests". I'll explain why I call them "legacy", eventhough the code they test is relatively new.

The first test method "TestMethodX" tested MethodX that is located on a Presenter class (we use the Model-view-presenter pattern). We ran it and failed. Ok, lets try to fix it:

1. Following good design principles on the ModelViewPresenter pattern, your method should have just one responsibility so it has only one reason to change. TestMethodX was long, had a lot of data initialization and used Mock classes in a very confusing way. There were lots of repeated statements and wasn't self documented (or had any code comments whatsoever).

2. On an Object Programming style, you should assign responsabilities to different objects instead of having a large procedural method. The way, your design is loosely coupled and highly cohesive. That's a good thing. But MethodX was very long. It used the View class on strange ways and had lots of ifs, whiles and calls to other private methods. When you try to test such a method, I assure you you'll have a hard time understanding it. It's not fun.


Long methods are evil on several grounds:
  • They're hard to understand
  • They're hard to change
  • They're hard to reuse
  • They're hard to test
  • They have low cohesion
  • They may have high coupling
  • They ten to be overly complex
Often programmers ask, how "long" is a long method? A method is too long if
  • you have to scroll down to look at the complete method
  • it takes several minutes to understand
  • you can't easily write an automated test for it
  • you can't state the one prominent purpose for the method
3. The method wasn't developed on a TDD style for sure. TDD leads to simple design and promotes clear cohesive code. So, the programmer that did the tests, obviuously did them afterwards and just as a formalism. The final result was some test code that didn't add any value to the development process and to the product itself. Honestly, the original developer wasted his time doing that tests.

After reading the unit test and the tested code, we had two options:

1. To make the unit test pass and have it constantly verifying a confusing designed class.
2. To delete the unit test and just face the truth that the tested method should be redesigned.

At the end, we decided to delete the unit test cause its existence added no value at all. I'd really like to undeline the concept that Unit tests are a strong resource for helping in your design. The enfasis shouldn't be on Test, but on Design. They are not just a formalism, they are useful and the final result is great code. That's what we are talking about.

So, if you are a developer, please give it a try to TDD, write code in responde to a unit test. Test first, test early. Make small methods and write human readable code. We, the rest of the world, would really apreciate it! :-)

Wednesday, August 12, 2009

Stop writing bugs...

I read somewhere that the best way to solve bugs in our software is to avoid introducing them in the first place.

Too obvious?

Well, if you think about it, the one responsible for writing a bug is the programmer itself. Software doesn't breed bugs by itself. Humans put them there, and humans suffer the consequences: ugly code, broken builds, unhappy users, stressed programmers.

So, IMHO, the best way for avoiding bugs is to do TDD... In Test Driven Development, you write software according to a specific automated requeriment (unit test) and nothing more. Your design becomes more cohesive, simple and mantainable. I love automation, so I've always liked the idea of having code verifying code. I remember the first time I used JUnit (2000? maybe). Wow, the simple example of automating the tests of a calculator java class made me realize that there was more to writing software than just compiling and nice screens.

TDD is about automated requirements, good design, cool automation and fewer bugs. If you like the idea, maybe you can benefit from How to write good tests. Top 5 considerations to build software without defects. Oh! And if you can, please read James Shore The Art of Agile Development. Its really good in showing the big picture of what software development is about and how eXtreme Programming can help.

Wednesday, May 20, 2009

New book! The art of unit testing


Hi, I just read the post from Roy Osherove where he anounces that his book "The art of UNit testing" is published and you can purchase it on PDF or print book.

He says that he wished he could haave had this book when he started writing his firts tests. When Roy published his first chapter on PDF for free I gladly downloaded it and have used it to start teaching developers on what unit testing is. So, I am sure his book is really useful for anyone who likes unit tests or works with Test Driven Development.

Wanna give it a try? I will!

Check this brief from http://www.manning.com/osherove/ where you can buy it:

Unit testing, done right, can mean the diff erence between a failed project and a successful one, between a maintainable code base and a code base that no one dares touch, and between getting home at 2 AM or getting home in time for dinner, even before a release deadline.

The Art of Unit Testing builds on top of what's already been written about this important topic. It guides you step by step from simple tests to tests that are maintainable, readable, and trustworthy. It covers advanced subjects like mocks, stubs, and frameworks such as Typemock Isolator and Rhino Mocks. And you'll learn about advanced test patterns and organization, working with legacy code and even untestable code. The book discusses tools you need when testing databases and other technologies. It's written for .NET developers but others will also benefit from this book.

You can also preorder it on Amazon.com.

Tuesday, January 13, 2009

Unit Testing in VB.NET – with Typemock Isolator (with a free license offer)

Programming Visual Basic applications?

Typemock have released a new version of their unit testing tool, Typemock Isolator 5.2. 
This version includes a new friendly VB.NET API which makes Isolator the best Isolation tool for unit testing A Visual Basic (VB) .NET application.

Isolator now allows unit testing in VB or C# for many ‘hard to test’ technologies such asSharePoint, ASP.NET MVC, partial support for Silverlight, WPF, LINQ, WF, Entity Framework, WCF unit testing and more.

Note that the first 25 bloggers who blog this text in their blog and tell us about it, will get a Free Full Isolator license (worth $139). If you post this in a VB.NET dedicated blog, you'll get a license automatically (even if more than 25 submit) during the first week of this announcement.

Go ahead, click the following link for more information on how to get your free license.

_