By Your Command (Line)

July 12, 2013

Last week, we saw an example of Dynamic Test Discovery in Fixie, in which the test runner can be made to run a different set of tests each time, depending on context. A Fixie convention could make decisions based on how the test run was initiated. This week, I'll demonstrate a similar feature which takes advantage of yet more context. For this example, our convention will get to make decisions based on the command line arguments used to kick off the test run.

Today's code sample works against Fixie 0.0.1.70. The customization API is in its infancy, and is likely to change in the coming weeks.

Before this build, Fixie.Console.exe treated all of its arguments as paths to test assembly files. As of Fixie 0.0.1.70, you can also specify arbitrary key/value pairs on the command line:

The console runner converts the named arguments into an ILookup<string, string>. ILookup objects are similar to a dictionary, except that each key can hold any number of values instead of just one.

Let's take advantage of this ILookup collection in order to implement NUnit-like categories. Consider a test class with several tests, some of which have been categorized with attributes:

Fixie has no idea what these category attributes mean, and it has no idea what any of the command line key/value pairs mean. Fixie's responsibility in the matter is simply to pass the ILookup along so that your custom conventions can make use of them however you see fit. Along with test classes like CategorizedTests, my test assembly contains my own category attributes:

Beside these category types, we define a custom convention to take advantage of them:

As we saw last week, our convention class can optionally accept a RunContext in its constructor. Now, this RunContext includes the ILookup of command line arguments. This convention says that a method in a test class should be treated as a runnable test case when a) no categories have been requested or b) categories have been requested and the method in question has at least one matching category attribute.

Let's run our tests a few times, with different command line arguments:

Oh, dear. When creating this demo project, I discovered a bug with the way the assembly path arguments are treated. If you use relative file paths, Fixie will fail to find them, and will produce a completely useless error message. It incorrectly alters the current directory before resolving the paths. That's easy to fix, but if you try this demo out yourself against this build, be sure to use an absolute path like the example above. Yay for dogfooding!

Ushering arbitrary key/value pairs from the command line to your custom conventions is a very small feature, but one that opens up an important door for open-ended customization.