Parameterized testing with Enumerations using Fixie: part 1
Situation: We want to run the same test for multiple values of an Enumeration class. For example:
- your SUT is a method whose output value depends on the item’s status; and you want to test for each possible status
- your SUT is the handler for an Edit form, and you want to run the test for each possible value in a select list
The NUnit solution:
NUnit has a
TestCaseSource attribute that lets you do this easily. Any field, property or method that returns an IEnumerable can be used to send in parameters to your test case.
Just define your data source:
And add the data source as a
TestCaseSource attribute to your test method. Your test will now run once for each value in the
I love this feature in NUnit, and wanted to use it for another project that uses Fixie, a convention-based testing framework from my colleague Patrick Lioi. (p.s. Check it out if you haven’t already, it’s now at version 1.0!)
The Fixie Solution
Looking for ways to do this, I found an example in Fixie’s documentation that lets you parameterize your tests with a custom
In this example the test will run twice - once for each repetition of the
This is close, but not quite what I envisioned, because for multiple parameters it gets messy quickly. Let’s say my Edit form had two select lists - one with 3 options and another with 2 - I would then need 6 Input attributes on my test method! Not to mention, what happens if I add a 3rd select list to the form, or some new options to my select list? A maintenance nightmare, that’s what:
What I’d really like is a convention that tells Fixie when some Enumeration parameters are being passed in, gets all the values of each enumeration, does the cartesian product, and runs the test for each combination of enumeration values:
Compare this to the previous screenshot. This test does not need to change anymore if you add another value to the Enumeration. As an added benefit, you get an actual Enumeration class instead of just a string/int/short value as your parameter.
The following gist shows how to add an
EnumeratedInput attribute to Fixie conventions, and use it in a test.
Example domain with 2 enumerations:
Assume we now have a domain with these two enumerations:
Example usage in Fixie tests
In the gist below, we have added an
EnumeratedInput attribute to the test, which takes 2 parameters. The result will run n1*n2 times, where n1 and n2 are the number of enumerations.
We’re still not all the way there; since our ultimate goal is to not just use enumerations, but to be able to define our own test case sources. But… we now know how to write a parameterized test using Fixie and how to write a convention that recognizes that parameter. This will come in handy for our next step. Stay tuned for the next blog post!
Thanks: Hat-tip to Sharon Cichelli and Patrick Lioi for helping me on this!
Update: next blog post is here!