Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Scott Keck-Warren:
Making dataProviders More Maintainable
Sep 30, 2015 @ 14:44:18

Scott Keck-Warren has a quick post to his site sharing a method for keeping data providers maintainable in your unit tests. Data providers are a quick way to retest the same logic with several different types of data and not have an individual test for each.

I’m a big fan of using PHPUnit’s data providers feature because it allows you to easily run a lot of data through the same kinds of tests over and over again without having a bunch of duplicate code sitting around. But they aren’t always the easiest thing to come back to an understand.

He briefly introduces how data providers are used in PHPUnit testing, including a brief code example. The errors that can come up with this common setup can be cryptic to debug. He recommends a slight alteration to the data provider return structure to use an associative array instead of a single-level array. This way, if there's an error the resulting message refers to the index, not just a number making a bit more sense and aids in debugging.

tagged: dataprovider maintainable phpunit tip associative array

Link: http://www.thisprogrammingthing.com/2015/making-dataproviders-more-maintainable/


Trending Topics: