Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Moodlemoot Australia 2016 Conference Perth
1. MOODLEMOOT AUSTRALIA 2016
MOODLEMOOT AUSTRALIA 2016 - PERTH 27th - 29th SEPTEMBER
Presented by Andrew Nicols, Senior Analyst Developer / Integrator
dataProvider
2. MOODLEMOOT AUSTRALIA 2016
– Feature of PHPUnit
– Available in all supported versions of Moodle
– Run the same test for multiple sets of data
dataProvider
13. MOODLEMOOT AUSTRALIA 2016
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
Hi
Who I am
Running this presentation in Pico format, and the slides are on auto
DataProviders are an awesome feature of PHPUnit since PHPUnit 4.0
We use 4.8 in stables and 5.4 in master - all supported versions of Moodle support them
Allows you to run the same test for multiple sets of data easily and without much duplication
If you are writing tests like this, or have tests like this in your code…
Where you repeat the same thing
Over, and over again...
Data Providers are for you!!!
Very easy to set up and start using
Use the @dataProvider annotation
Just points to a public callable function
Your provider just needs to return an array of arrays
Each key in the outer array is a piece of data being tested
Each key in the inner array is an argument to the test
You can have many parameters… but
Too many is probably a sign that you’re doing it wrong
your test is too complex
or testing too many things
You can also name each test in the provider.
I’d really recommend doing so. It will make your life much easier
Especially when you come back 18 months later and try to understand why you wrote this test case
(don’t forget it’s still an array.. Can’t use duplicate names)
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 duplication
Clear separation of the test, and the data being tested
And when we run it, it looks like this
Usually the same speed, though will be slower if your test does insert data
But a realistic representation of the test
And you can filter on a specific piece of data when running the tests (or wildcard capturing multiple)
It will tell you which testcase failed
Though we hope that none do of course
Use naming to your advantage
The dataProvider provider function must be a public method on the class
It cannot contain DB/FS setup because it is called very early on.
That’s because setUp happens before each of your tests
And phpunit needs to calculate the total number of tests before it runs
Great because they:
Reduce code duplication
Increase 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
You can also do some funkier stuff with them
Like 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