Generating Test Cases at Runtime


Last time, we saw how Fixie can integrate with AutoFixture. That was a situation in which parameterized tests were meant to be called once. They were parameterized because the producer of the inputs was interesting while the count was uninteresting.

Before that, we saw how a convention could instead cause parameterized test methods to be called multiple times, using attributes as a source of multiple inputs. That example was a little stifling: you have to know at compile time how many scenarios you are testing, and you can only specify compile-time constants in attributes.

Today, we’ll see an example of parameterized tests which are called multiple times, using values generated at runtime. Consider a test class with two test methods and some static Person-generator methods:

Without a custom convention, Fixie fails to invoke the parameterized test:

Let’s define a custom convention which considers single-argument test methods. For a test method that takes in some type T, it will find all the static methods returning IEnumerable, call them all, and pass in the many T objects into the test method one at a time:

Now, we see that our two test methods produce 5 individual test cases.

Oh dear. Can you spot the bug? It’s possible to write test methods that never get invoked. In our next episode, we’ll cover the bug as well as an improvement in Fixie that will prevent such subtle surprises while simplifying parameterized test conventions.

AutoFixie