moodle moot australia 2016 - developer jam - phpunit dataprovider
TRANSCRIPT
Presented by Andrew Nicols, Senior Analyst Developer / IntegratordataProvider
MOODLEMOOT AUSTRALIA 2016 - PERTH 27th - 29th SEPTEMBER
MOODLEMOOT AUSTRALIA 2016
HiWho I amRunning this presentation in Pico format, and the slides are on auto
Feature of PHPUnit Available in all supported versions of Moodle Run the same test for multiple sets of datadataProvider
MOODLEMOOT AUSTRALIA 2016
DataProviders are an awesome feature of PHPUnit since PHPUnit 4.0We use 4.8 in stables and 5.4 in master - all supported versions of Moodle support themAllows you to run the same test for multiple sets of data easily and without much duplication
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
If you are writing tests like this, or have tests like this in your codeWhere you repeat the same thingOver, and over again...Data Providers are for you!!!
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
Very easy to set up and start usingUse the @dataProvider annotationJust points to a public callable function
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
Your provider just needs to return an array of arraysEach key in the outer array is a piece of data being tested
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
Each key in the inner array is an argument to the testYou can have many parameters butToo many is probably a sign that youre doing it wrongyour test is too complexor testing too many things
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
You can also name each test in the provider.Id really recommend doing so. It will make your life much easierEspecially when you come back 18 months later and try to understand why you wrote this test case(dont forget its still an array.. Cant use duplicate names)
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
With a dataProvider, the test from before now looks like this.Although it can take up more lines,The test itself does not and there is no code duplicationClear separation of the test, and the data being tested
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
And when we run it, it looks like thisUsually the same speed, though will be slower if your test does insert dataBut a realistic representation of the test
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
And you can filter on a specific piece of data when running the tests (or wildcard capturing multiple)It will tell you which testcase failedThough we hope that none do of courseUse naming to your advantage
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
The dataProvider provider function must be a public method on the classIt cannot contain DB/FS setup because it is called very early on.Thats because setUp happens before each of your testsAnd phpunit needs to calculate the total number of tests before it runs
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
Great because they:Reduce code duplicationIncrease failure relevance and give a more detailed picture upon failure (all failures and not just one)Make it easier for you to add additional test cases
dataProvider Use them to parse a CSV file Have them extend Iterator for larger files Process directories of fixtures Have many dataProviders share the same dataset
MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016
You can also do some funkier stuff with themLike user something which extends Iterator Interface (as long as each key is an Array)Great if you have a set of existing tests in a CSV, or other files
For more information contact:moodle.com
MOODLEMOOT AUSTRALIA 2016