Hi all,
Hope you are doing well. I wish to add some thoughts on writing better unit tests.
Many a times we need to pass parameters to the Unit Test cases that we write. This can be accomplished using TestCase attribute, such as this.
But the TestCase attribute requires a constant argument. What if we need to assemble a complex Object and pass to the unit test case?
1. Calling a helper method from the TestCase parameter to assemble an object? - No (As only constants can be passed as Positional Parameters to attributes)
Work Around:
This can be achieved by exploiting the power of factory pattern.
Consider the following test case, where we need to pass different employee instances (may be even Employee is an abstract class and has two Childs, Manager and Labour. )
The test case for both the Concrete classes can be written in one neat way as above.
The Factory class which serves as the input container is as follows .
This approach avoids the coupling between unit tests and the input sets , and more over reduces the redundant code by a large margin.
I shall blog about the Combinatorial attribute in the next post.
Thanks,
Vijay