views:

715

answers:

10

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.

+3  A: 

You could possibly re-factor the method into parts that are easier to unit test and write the unit tests for the parts. Then the unit tests for the whole method only need to concentrate on integration issues.

ConcernedOfTunbridgeWells
It's hard to imagine refactoring this into a simpler method. I know the OP characterized it as "complex", but it sounds pretty simple to me.
Ned Batchelder
Yes, the basic problem is pretty simple. I used the term complex with regards to the possible inputs and results. However, re-factoring is certainly an option in other problems as a way of simplifying unit tests.
Jeff Yates
+3  A: 

I believe that your solution is fine, despite using a XML file (I would have used a plain text file). But a more used tactic is to just test limit situations, like using, in your case, a entry value of -360, 360, -361, 361 and 0.

Seiti
Thanks. Testing the limits seems like sound advice.
Jeff Yates
+19  A: 

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.

Bill the Lizard
Thanks. You've really helped!
Jeff Yates
+1 for the idea of testing with pre-computed input-output test vectors - focusing on the boundry and edge cases.
Ian Boyd
+3  A: 

I prefer to do the following.

  1. Create a spreadsheet with right answers. However complex it needs to be is irrelevant. You just need some columns with the case and some columns with the expected results.

    For your example, this can be big. But big is okay. You'll have an angle, a bearing and the resulting compass point value. You may have a bunch of intermediate results.

  2. Create a small program that reads the spreadsheet and writes the simplified, bottom-line unittest cases. You want your cases stripped down to

    def testCase215n( self ):
        self.fixture.setCourse( 215 )
        self.fixture.setBearing( 45 )
        self.fixture.calculate()
        self.assertEquals( "N", self.fixture.compass() )
    

[That's Python, the same idea would hold for C#.]

The spreadsheet contains the one-and-only authoritative list of right answers. You generate code from this once or twice. Unless, of course, you find an error in your spreadsheet version and have to fix that.

I use a small Python program with xlrd and the Mako template generator to do this. You could do something similar with C# products.

S.Lott
Seems like this could be done much simpler by putting the data in a list, and iterating it at run-time to invoke tests, rather than generating the code.
Ned Batchelder
@Ned Batchelder: Absolutely true. And perhaps simpler. But I prefer my unit tests do nothing but unit test. A unit test which reads a file or computes a preferred answer doesn't "feel" right to me. Not a great reason, is it?
S.Lott
An interesting idea, S.Lott. This might be useful in some scenarios. Thnaks.
Jeff Yates
A: 

So I took a software testing class link text and basically what you want is to identify the class of inputs.. all real numbers? all integers, only positive, only negative,etc... Then group the output actions. is 360 uniquely different from 359 or do they pretty much end up doing the same thing to the app. Once there do a combination of inputs to outputs.

This all seems abstract and vague but until you provide the method code it's difficult to come up with a perfect strategy.

Another way is to do branch level testing or predicate coverage testing. code coverage isn't fool proof but not covering all your code seems irresponsible.

There should be no need to see the method code when developing a unit test. If you write the test to the implementation, you're not properly testing the requirements. I don't think the question I posed is specific to the example I gave, hence I left out nitty-gritty details. Thanks.
Jeff Yates
+2  A: 

If you can think of a completely different implementation of your method, with completely different places for bugs to hide, you could test against that. I often do things like this when I've got an efficient, but complex implementation of something that could be implemented much more simply but inefficiently. For example, if writing a hash table implementation, I might implement a linear search-based associative array to test it against, and then test using lots of randomly generated input. The linear search AA is very hard to screw up and even harder to screw up such that it's wrong in the same way as the hash table. Therefore, if the hash table has the same observable behavior as the linear search AA, I'd be pretty confident it's correct.

Other examples would include writing a bubble sort to test a heap sort against, or using a known working sort function to find medians and comparing that to the results of an O(N) median finding algorithm implementation.

dsimcha
I did consider this as an option, but it didn't sit right with me. I have used this for other unit tests that have less input variations.
Jeff Yates
A: 

One approach, probably one to apply in combination with other method of testing, is to see if you can make a function that reverses the method you are testing. In this case, it would take a compass direction(northeast, say), and output a bearing (given the bearing for north). Then, you could test the method by applying it to a series of inputs, then applying the function to reverse the method, and seeing if you get back the original input.

There are complications, particularly if one output corresponds to multiple inputs,but it may be possible in those cases to generate the set of inputs corresponding to a given output, and test that each member of the set (or a certain sample of the elements of the set).

The advantage of this approach is that it doesn't rely on you being able to simulate the method manually, or create an alternative implementation of the method. If the reversal involves a different approach to the problem to that used in the original method, it should reduce the risk of making equivalent mistakes in both.

Silverfish
An interesting idea, thanks.
Jeff Yates
+1  A: 

You could try orthogonal array testing to achieve all-pairs coverage instead of all possible combinations. This is a statistical technique based on the theory that most bugs occur due to interactions between pairs of parameters. It can drastically reduce the number of test cases you write.

moffdub
Thanks, I'll look into it. It sounds interesting.
Jeff Yates
+1  A: 

Not sure how complicated your code is, if it is taking an integer in and dividing it up into 8 or 16 directions on the compass, it is probably a few lines of code yes?

You are going to have a hard time not re-writing your code to test it, depending how you test it. Ideally you want an independent party to write the test code based on the same requirements but without looking at or borrowing your code. This is unlikely to happen in most situations. In this case that may be overkill.

In this specific case I would feed it each number in order from -360 to +360, and print the number and the result (to a text file in a format that can be compiled into another program as a header file). Visually inspect that the direction changes at the desired input. This should be easy to visually inspect and validate. Now you have a table of inputs and valid outputs. Next have a program randomly select from the valid inputs feed it into your code under test and see that the right answer comes out. Do a few hundred of these random tests. At some point you need to validate that numbers less than -360 or greater than +360 are handled per your requirements, either clipping or modulating I assume.

dwelch
You are right that I need to test values outside my permitted limits, and I like your ideas on how I can generate and use my test data. THanks.
Jeff Yates
A: 

Psuedocode:

array colors = { red, orange, yellow, green, blue, brown, black, white }
for north = -360 to 361
    for bearing = -361 to 361
        theColor = colors[dirFunction(north, bearing)] // dirFunction is the one being tested
        setColor (theColor)
        drawLine (centerX, centerY,
                  centerX + (cos(north + bearing) * radius),
                  centerY + (sin(north + bearing) * radius))
        Verify Resulting Circle against rotated reference diagram.

When North = 0, you'll get an 8-colored pie chart. As north varies + or -, the pie chart will look the same, but rotated around that many degrees. Test verification is a simple matter of making sure that (a) the image is a properly rotated pie chart and (b) there aren't any green spots in the orange area, etc.

This technique, by the way, is a variation on the world's greatest debugging tool: Have the computer draw you a picture of what =IT= thinks it's doing. (Too often, developers waste hours chasing what they think the computer is doing, only to find that it's doing something completely different.)

Olie
This is a good idea for manual testing but not so useful for automated unit tests. I like your thinking though and I agree, getting the computer to paint pictures of what it is doing is a very powerful tool.
Jeff Yates
This can certainly be automated. In addition, it completely solves the "don't use the same algorithm in the test that you use in the code" problem.For half-automated, you could visually check a set of reference images, then save those to a "correct answer" cache.
Olie
How would you automate this test such that a person doesn't need to evaluate the result? Do image comparisons? How do you make sure the image comparison is correct?
Jeff Yates