I am using this for years:
Repeat! This is for me the most efficient as you only load your test data once; NOT for every tests. Of course you will have to think harder of your tests scenario.
For example, think about a crud test:
Test1:
- Check that list has 1 item (loaded by fixtures)
Test2:
- Create an item
- Check that list has 2 items
Test3:
- Delete an element
- Check that the list has 1 item
So the tests must be executed in this order exactly. If you are loading fixtures between each test you don't have to take care of that order but it will make your tests slow.
I feel loading fixtures once is better because it will act as a user would normally do: create, delete, update items... And therefore you can check that there is no side effect between each action.
My load fixtures script:
#!/bin/bash
echo "##########################################################################"
echo "# Refresh data model, reload all reference data, load fixtures, #"
echo "# validate schema for the dev env. #"
echo "##########################################################################"
php bin/console doctrine:database:create --if-not-exists --env=dev
php bin/console doctrine:schema:drop --force --env=dev
php bin/console doctrine:schema:create --env=dev
php bin/console doctrine:schema:validate --env=dev
php bin/console doctrine:fixtures:load -n --env=dev
echo -e " --> DONE\n"
Or if you want to load a database for SQL files, use:
php bin/console doctrine:database:import db.sqb --env=dev
Instead of the load fixtures command.
Then, to launch the tests:
./bin/simple-phpunit --debug --verbose $1
$1
is an argument to specify the test suite to load. (main, front, API, backend...) which you can parameter in your phpunit.xml.dist
file. (you can omit it to run all the tests)