Saturday, February 1, 2014
Using PHPUnit data provider for less code and greater coverage
Posted by Geshan Manandhar at Saturday, February 01, 2014 Tags: PHP , Programming , Technology , Testing , Tutorial
Tests also help a lot when you re-factor some code or write new features. If all the tests are passing you are quite sure that nothing is breaking. Seeing all tests come out green is a very good sight for us software engineer.
Testing in PHP with PHPUnitUnit testing and testing as a whole is not a new thing in PHP, PHPUnit the de facto unit testing framework in PHP started in 2001, The chart below shows that popularity of PHPUnit has really grown over the past years in comparison to Simple Test another PHP testing framework.
|Source: Google Trends|
The difference between a good programmer and a bad programmer is that the good programmer uses tests to detect mistakes as soon as possible. The sooner you test for a mistake the greater your chance of finding it and the less it will cost to find and fix.It is true the sooner the problem is detected and fixed the better it is, same goes when some code is re-factored, a new feature is added or a bug resolved.
Data Provider in PHPUnit
There are may features of PHPUnit, which can help in writing comprehensive unit testing and moving toward the direction of test driven development (TDD). Among them, data providers provide arbitrary arguments to a test function so that same code can be used to test multiple scenarios and possible cases. In this very simple example I will use a part of checkout. The logic is very simple:
- If the payment method is "Cash", apply a cash on delivery fee of 5.0
- If the payment method is "Credit Card", do not apply a cash on delivery fee.
The part of the checkout class is given below:
Source: Github repository
The test for the above checkout class is given below:
Source: Github repository
What makes using data provider in this example relevant and optimal?
- As the test needs to be run for both "Cash" and "Credit Card" the only thing it changes here is the addition of cash on delivery fee.
- There is just one test with the payment method being passed as a parameter, saving us from writing X no of tests for X no. of payment methods. So less code and more coverage.
- The test is clear concise and understandable.
Things to consider in the code:
- The test runs the as many times as the data set provided by the provider function.
- @dataProvider annotation is necessary in the test to get the data sets from the provider function.
- When writing the provider function, its better to comment who is using the provided data sets and the order of variables (check the comment of paymentMethodProvider method).
Whenever you write tests and think, "Why am I writing a new test for the similar thing. I guess there can be a way to write less test/code and cover all my test cases." Then is time to use data providers in PHPUnit. You save on time and effort to cover the cases, a new case is just another array in the provider function, even in case of new test cases being added its a one line change given the tests pass. I hope this helps you to write less test code and achieve more coverage.