"If a user tries to total a string column they get a meaningful error message."
This is going to require some new functionality to the test framework; as for this test to pass we will need the Alteryx module for it to fail (and not just fail, but fail with a very specific message). I am going to start with how a user of the test framework (i.e. a macro developer) would do this and then we'll take a quick look at how the test framework works for this feature.
Write a Failing Test
For this test we will use a new macro in the test framework the Expect Error macro:
And we write a test which looks like this:
- The Text Input has a single Text field called field 1 containing "Some Text"
- The Expect Error macro has a single configuration option "Expected Error Message" which we set to the error message we expect to get in this scenario "Not possible to total Field1 as it is a non-numeric type: String"
Now if we run our test runner module we get the following output
So we get a message telling us what error message we were expecting to get from which tool(s) and then messages telling us what error messages we actually got (in this case the standard formula message about trying to use a string when a number was expected).
We can make one slight improvement to this test, in that we know exactly which tool we are expecting the error message to come from, so we should specify that in the test. We do that by enclosing the Expect Error tool in a tool container with which ever tools we expect the error to come from:
Giving us the following log from the test runner:
Make The Test Pass
Having a failing test we can now add functionality to the macro to make it pass. Which is a fairly straightforward process:
- We already had a Field Info tool which gave us the types of all of our fields.
- New filter tool filters off any fields of type String.
- Then a message tool throws the correct error message to the macro user configured like so:
Run the Test Runner again and:
Success! We have written our first unit test which tests for an error.
So How Does It Work?
I said at the start that after we had used the new functionality we would take a look at how it worked. This detail is not necessary to be able to successfully use the test framework, but might be interesting to see what's going on behind the scenes.
The Expect Error Macro
The contents of the Expect Error Macro are probably the most boring Alteryx macro you will ever see. It looks like this:
That's right apart from the question it is empty! All of the "magic" happens in the Test Runner macro.
The Test Runner Macro
This is looking a bit more complicated than last time we saw it and actually probably could do with being re-factored into a few sub macros:
- The macro here looks inside each of the Test Modules and parses out the Tool XML. It is looking for Test Modules which contain the Expect Error macro.
- Modules which don't contain the Expect Error macro go this top route and get run as we saw previously. If the module errors it fails the test and reports to the output log.
- Modules which do contain the Expect Error macro are also run with the Runner macros but this route the test fails if the test module runs without errors.
- This section again parses out the module XML and works out what error messages and from what tools the Expect Error macro is expecting.
- This join tool matches the expected error messages against the error messages which came out of the log.
- The final section of tools deals with reporting any mismatches between the expected and actual error messages and reporting the success or failure of the test.