Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test complex methods

I have a method that, given an angle for North and an angle for a bearing, returns a compass point value from 8 possible values (North, NorthEast, East, etc.). I want to create a unit test that gives decent coverage of this method, providing different values for North and Bearing to ensure I have adequate coverage to give me confidence that my method is working.

My original attempt generated all possible whole number values for North from -360 to 360 and tested each Bearing value from -360 to 360. However, my test code ended up being another implementation of the code I was testing. This left me wondering what the best test would be for this such that my test code isn't just going to contain the same errors that my production code might.

My current solution is to spend time writing an XML file with data points and expected results, which I can read in during the test and use to validate the method but this seems exceedingly time consuming. I don't want to write a file that contains the same range of values that my original test contained (that would be a lot of XML) but I do want to include enough to adequately test the method.

  • How do I test a method without just reimplementing the method?
  • How do I achieve adequate coverage to have confidence in the method I am testing without having to have test points for all possible inputs and results?

Obviously, don't dwell too much on my specific example as this applies to many situations where there are complex calculations and ranges of data to be tested.

NOTE: I am using Visual Studio and C#, but I believe this question is language-agnostic.

like image 528
Jeff Yates Avatar asked Nov 02 '08 23:11

Jeff Yates


People also ask

How do you write a complex junit test case?

Create member variables in the test class to hold the test collaborators and set their state in the setup method. This gives the test methods the ability to change them as needed for a given test. In each test, change the test fixture if necessary to achieve the satisfy the conditions of the test.

Which method is used for unit testing?

Unit Testing Techniques:Black Box Testing - Using which the user interface, input and output are tested. White Box Testing - used to test each one of those functions behaviour is tested. Gray Box Testing - Used to execute tests, risks and assessment methods.


1 Answers

First off, you're right, you do not want your test code to reproduce the same calculation as the code under test. Secondly, your second approach is a step in the right direction. Your tests should contain a specific set of inputs with the pre-computed expected output values for those inputs.

Your XML file should contain just a subset of the input data that you've described. Your tests should ensure that you can handle the extreme ranges of your input domain (-360, 360), a few data points just inside the ends of the range, and a few data points in the middle. Your tests should also check that your code fails gracefully when given values outside the input range (e.g. -361 and +361).

Finally, in your specific case, you may want to have a few more edge cases to make sure that your function correctly handles "switchover" points within your valid input range. These would be the points in your input data where the output is expected to switch from "North" to "Northwest" and from "Northwest" to "West", etc. (don't run your code to find these points, compute them by hand).

Just concentrating on these edge cases and a few cases in between the edges should greatly reduce the amount of points you have to test.

like image 161
Bill the Lizard Avatar answered Sep 25 '22 10:09

Bill the Lizard