Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing a large method

Following Test-Driven Development that is.

I've recently implemented a algorithm (A*) that required a clean interface. By clean all I want is a couple of properties and a single search method.

What I've found hard is testing the search method. It contains around five steps but I'm essentially forced to code this method in one big go which makes things hard.

Is there any advice for this?

Edit

I'm using C#. No I don't have the code at hand at the moment. My problem relies in the fact a test only passes after implementing the whole search method - rather than a step in the algorithm. I naturally refactored the code after but it was implementing I found hard.

like image 828
Finglas Avatar asked Oct 25 '09 13:10

Finglas


People also ask

Which method is used in unit testing?

Unit tests can be performed manually or automated. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method to unit tests. Automated approaches commonly use a testing framework to develop test cases.

Should unit tests be small?

A unit can be almost anything you want it to be -- a line of code, a method, or a class. Generally though, smaller is better. Smaller tests give you a much more granular view of how your code is performing.

What is unit testing in big data?

A unit test aims to check whether a part of your code operates in the intended way. Writing them has the following benefits: Reduces bugs when developing new features or when changing the existing functionality. Prevents unexpected output. Helps detecting edge cases.


1 Answers

If your steps are large enough (or are meaningful in their own right) you should consider delegating them to other smaller classes and test the interaction between your class and them. For example if you have a parsing step followed by a sorting step followed by a searching step it could be meaningful to have a parser class, a sorter class, etc. You would then use TDD on each of those.

No idea which language you're using, but if you're in the .net world you could make these classes internal and then expose them to your test class with "internals visible to" which would keep them hidden.

If the steps are small AND meaningless on their own then tvanfosson's suggestions are the way to go.

like image 97
FinnNk Avatar answered Sep 20 '22 12:09

FinnNk