Unit testing a method with many possible outcomes

Posted by Cthulhu on Stack Overflow See other posts from Stack Overflow or by Cthulhu
Published on 2010-12-24T13:50:05Z Indexed on 2010/12/24 13:54 UTC
Read the original article Hit count: 216

Filed under:

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:

  • address
  • address port
  • address port path
  • address path
  • address action
  • address path action
  • address port action
  • address port path action
  • address action params
  • address path action params
  • address port action params
  • address port path action params

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)

© Stack Overflow or respective owner

Related posts about unit-testing