I am preparing unit test cases, in which I am invoking methods with one or more arguments. For the purposes of a test case, exact values of certain arguments may not be important, as long as they are from an acceptable input range, or its sub-range (e.g. non-zero).
I want future readers of my test cases to understand my intent. I do not want them to spend time on figuring out why a certain value was used.
Has anyone established a solution to this? Do any testing frameworks provide abstractions for "don't care" values? Are there widespread coding conventions or techniques for that?
I am mostly interested in C, C++, and Python, but I believe that the question applies to many programming languages, and technologies.
One option is to use telling variable names.
aValidNumber
or validNumber
.anyString
.invalidPhoneNumber
, or anInvalidPhoneNumber
.The book xUnit Test Patterns also suggests the name dummy to indicate a value that's only present to satisfy a compiler or interpreter.
You can also use Test Data Builders to make it clear to readers which values are important, and which ones aren't.
Property-based frameworks such as QuickCheck (there are ports to many languages) typically take this further and use an abstraction called Arbitrary
, where an Arbitrary
instance is, as the name implies, an arbitrary, randomly generated value.