Home > database >  How to express "don't care" value for a function/method argument in a test
How to express "don't care" value for a function/method argument in a test

Time:11-11

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.

CodePudding user response:

One option is to use telling variable names.

  • If you have a valid number, you could call the variable aValidNumber or validNumber.
  • If it's any string, you could call the variable anyString.
  • If the value is, on the other hand, an invalid phone number, then use 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.

CodePudding user response:

"As long as they are from an acceptable input range" is not the same "don't care".

If a parameter value needs to be valid, but the actual value does not matter for the test case, I usually define the value as a constant or variable named as such, that it is clear to the reader that the value does not matter for the case. Example: A_VALID_USER_NAME, which communicates that the test case is about calling the function/method with a valid user name.

  • Related