Reputation: 3169
I've built a simple~ish method that constructs an URL out of approximately 5 parts: base address, port, path, 'action', and a set of parameters. Out of these, only the address part is mandatory, the other parts are all optional. A valid URL has to come out of the method for each permutation of input parameters, such as:
andsoforth. The basic approach for this is to write one unit test for each of these possible outcomes, each unit test passing the address and any of the optional parameters to the method, and testing the outcome against the expected output.
However, I wonder, is there a Better (tm) way to handle a case like this? Are there any (good) unit test patterns for this?
(rant) I only now realize that I've learned to write unit tests a few years ago, but never really (feel like) I've advanced in the area, and that every unit test is a repeat of building parameters, expected outcome, filling mock objects, calling a method and testing the outcome against the expected outcome. I'm pretty sure this is the way to go in unit testing, but it gets kinda tedious, yanno. Advice on that matter is always welcome. (/rant)
(note) christmas weekend approaching, probably won't reply to suggestions until next week. (/note)
Upvotes: 3
Views: 566
Reputation: 24132
Since you normally would expect only one unique outcome, no matter what order is given the parameters, I suggest one test for all the possibilities. In my code sample, I use NUnit Testing Framework, so it is yours to find out how to make the equivalent test with your testing framework.
[TestCase("http://www.url.com")]
[TestCase("http://www.url.com", 21)]
[TestCase("http://www.url.com", 24, @"c:\path")]
public void TestingMethod(string address, params object[] address) {
// Do your tests accordingly here...
}
So, the TestCaseAttribute
(using NUnit), is the right tool for the job.
Sure you'll need to determine what parameter value is at what index of the parameter array. I make it an object[]
, since I suppose that the different parameters have different data type as well, and since we cannot determine the right order from start, then you'll have to find it out for yourself, though using the polymorphism.
Upvotes: 2