Saturday, October 22, 2016

Where are your input values coming from?

When design your test cases, you will have to decide what type of input values are want to use, but consider where your input values are coming form. The most obvious way of inject values are by passing into via the function parameters. It is logical and it is the formal method. However, that is only one of the ways of injecting values into it. The three most popular methods are.

Function Parameters -Function parameters are part of full definitions of the function. It is one of the fundamental ways of passing data into the function. The range of values that it can take are defined by its type. Function Parameters can also be used to get values out of the function. In C, the function parameters live on the stack memory, and its life time ends when the function ends.

Returned Values – The low level functions called by the your module under tests must be considered as an input. From a unit test perspective, the return values can be influenced via the mocked function. The range of values is dependent on its data type. In C, the function parameters live on the stack memory, and its life time ends when the function ends. The other thing to consider is that this method does not necessary inject the values at the start of your function under test.

Global Variables – variables used in the software modules that are accessible outside of the modules. This method is best avoided in complicated code bases as it tends to create a software module that is prone to unpredictable behaviour if the global variables are not controlled well. The global variable lives on the heap, and it is alive as long as the program is alive.

For most parts, the input methods comes down the how you have design your module. Some design guideline forbid the use of mechanism such as global variables, so make sure that your methods matches your design guidelines.

I am writing a Guide to Successful Unit Tests.
You can get it here at Leanpub. or here at Gumroad. and read about these topics and more.

No comments:

Post a Comment