5g:Testing

Description
5g functions must be thoroughly tested before releasing to others. Releasing a function that can fail can spell disaster for those who use our functions and for our reputation. Thorough testing is one of the best ways for ensuring our functions won't fail.

Learning Objectives
At the end of this course, learners will know:

Discussion

Testing is a proven technique for driving out errors. There are many things, and many ways to test. Let us discuss a few.

Inline Help Test
One of the first things, and easiest things to test is our function's inline help. Just type = followed by the function's name and don't enter any arguments. Examine the displayed inline help text. 

Scenario Tests
It is impossible to test for all scenarios for any problem that involves just a handful of numeric or date variables. What we can do is use sampling to create random values that reasonably represent the values a user might enter. An automated version of this approach is shown in the video at right and the add-in used to automate the tests is available here: BXL Tests.xlam.

The basic approach is to use one of Excel's random number generators: RAND, RANDARRAY, and RANDBETWEEN, to create sample values for each of our function's arguments and place the randomize values where they can be accessed by our 5g function and a non-LAMBDA, non-dynamic array solution that does the same thing. Compare results. If both methods produce identical results, the function may be correct. Repeat many times to confirm.

Creating Tests

Tests can be created in many ways but the basics are the same. 

Inline Help

Do not neglect testing inline help. In a separate worksheet, enter the 5g function without arguments and check the display:

Function Tests

This is the most common method I use for setting up tests. I am always humbled by this process as it seems my tests always fail at least once for simple functions, and dozens of times for complex functions. When my functions finally do pass at least a thousand tests, I feel assured it is probably right. To setup test with BXL Tests:

Additional Tests

Edge  Tests
These tests check for things the end user might do, and probably shouldn't. Examples include invalid dates, negative amounts where only positive amounts are allowed, text entered for numeric inputs, codes entered that are not valid, etc. Create tests for these cases. For invalid inputs, 5g Functions should fail with one of Excel's error conditions.

Orientation Test
We should remember that dynamic arrays can spill horizontally and vertically. If we want our function to work no matter which way the array spills, we must test it against both orientations.

Array vs Range Test
Dynamic arrays can spill into cells, or create an array within our function. In general, 5g functions are for ranges. But some can be used inside formulas and never spill into a range. This can cause some Excel functions to fail. The xIFS functions (SUMIFS, COUNTIFS, etc.) only work over ranges. If we intend our function to work inside a formula, we should test that it will work in that environment.

Summary

Testing is one of the best ways to uncover errors in our functions. The hard part of testing is thinking about all the ways our function might be used. When testing functions in timelines, we should consider how our function should respond when the start date is before the timeline start and the end date is after the timeline end.

Good testing requires that we not only think about what the user should do, but what the user shouldn't do, but could.