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 workbook used to automate the tests is available here: TestTemplate workbook.

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.

TestTemplate.xlsm

To use TestTemplate.xlsm follow these steps.

Here are some options for randomized values:

Number

Use this for inputs that require numbers with decimals such as currency amounts.

=LowValue + RAND() * (HighValue - LowValue)

Where LowValue is the lowest value that could possibly make sense for your function and HighValue is the highest.

Integer

Use this for inputs that require numbers with decimals such as currency amounts.

=RANDBETWEEN( LowValue, HighValue)

Where LowValue is the lowest value that could possibly make sense for your function and HighValue is the highest.

Date

Use this for inputs that require Dates.

=RANDBETWEEN( "2024-01-01", "2031-12-31")

Replace 2024-01-01 with the earliest date that could possibly make sense for your function and replace 2031-12-31 with the highest. Make sure leap year dates are included (if applicable)

Choose

Use this for inputs that can only be one of a few choices such as timing intervals like Day, Week, Month, Quarter, Year.

= CHOOSECOLS( TEXTSPLIT( "D,W,M,Q,Y", ","), RANDBETWEEN(1,5))

Replace "D,W,M,Q,Y" with all possible choices. Replace 5 with the number of possible choices. 

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.