I ve got a SF3 application and lot of functionnals tests. Before each tests we load and purge all fixtures. Time of all tests are so long. I would like to load fixtures just one time et truncate after last test.
Is it the good method to improve functionnal tests speed ?
Is there a php method in phpunit which is launched just one time before all tests ? (Because setUpBeforeClass is executed before each test)
An exemple of the setUpBeforeClass method in my test's classes.
class SearchRegisterControllerTest extends WebTestCase
{
/** @var Client $client */
private $client;
protected static $application;
public static function setUpBeforeClass()
{
$kernel = static::createKernel();
$kernel->boot();
$em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
$schemaTool = new SchemaTool($em);
$metadata = $em->getMetadataFactory()->getAllMetadata();
$schemaTool->dropSchema($metadata);
$schemaTool->createSchema($metadata);
/** @var Client $client */
$client = static::createClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$loader = new Loader();
$loader->loadFromDirectory('src/MyNameSpace/AppBundle/DataFixtures/ORM');
$purger = new ORMPurger();
$executor = new ORMExecutor($em, $purger);
$executor->execute($loader->getFixtures(), true);
}
Thanks in advance.
One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test.
Fixtures are used to load a "fake" set of data into a database that can then be used for testing or to help give you some interesting data while you're developing your application. This bundle is compatible with any database supported by Doctrine ORM (MySQL, PostgreSQL, SQLite, etc.).
PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.
You could implement a test listener.
tests/StartTestSuiteListener.php
namespace App\Tests;
class StartTestSuite extends \PHPUnit_Framework_BaseTestListener
{
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
{
// do initial stuff
}
}
Then enable enable the test listener in your phpunit.xml
config:
phpunit.xml
<phpunit
...
>
<listeners>
<listener class="App\Tests\StartTestSuiteListener">
</listener>
</listeners>
[...]
</phpunit>
In the same manner you could implement a endTestSuite (Check for all event listed in the doc)
Hope this help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With