Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPunit different bootstrap for all testsuites

Tags:

php

phpunit

<phpunit backupGlobals="false" colors="true">
    <testsuite name="app1" >
        <directory>./app1</directory>
    </testsuite>
    <testsuite name="app1" >
        <directory>./app2</directory>
    </testsuite>
</phpunit>

How can i make first and second testsuite load different bootstraps?

like image 739
Napas Avatar asked Mar 02 '12 14:03

Napas


3 Answers

What I did is to have a Listener.

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./phpunit_bootstrap.php"
     backupGlobals="false"
     backupStaticAttributes="false"
     verbose="true"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false"
     syntaxCheck="true">
    <testsuites>
        <testsuite name="unit">
            <directory>./unit/</directory>
        </testsuite>
        <testsuite name="integration">
            <directory>./integration/</directory>
        </testsuite>
    </testsuites>
    <listeners>
        <listener class="tests\base\TestListener" file="./base/TestListener.php"></listener>
    </listeners>
</phpunit>

Then TestListener.php

class TestListener extends \PHPUnit_Framework_BaseTestListener
{
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        if (strpos($suite->getName(),"integration") !== false ) {
            // Bootstrap integration tests
        } else {
            // Bootstrap unit tests
        }
    }
}
like image 89
AntonioHS Avatar answered Oct 22 '22 11:10

AntonioHS


You can create two different bootstrap file and two different configuration xml files

app1.xml

<phpunit bootstrap="app1BootstrapFile.php" colors="true">
    <testsuite name="app1" >
        <directory>./app1</directory>
    </testsuite>
</phpunit>

app2.xml

<phpunit bootstrap="app2BootstrapFile.php" backupGlobals="false" colors="true">
    <testsuite name="app2" >
        <directory>./app2</directory>
    </testsuite>
</phpunit>

To run:

$phpunit --configuration app1.xml app1/
$phpunit --configuration app2.xml app2/

If you run one lot of test more that the other (say app1), name the xml phpunit.xml and you can just run

$phpunit app1/
$phpunit --configuration app2.xml app2/

I do this with unit/integration tests.

like image 38
Aine Avatar answered Oct 22 '22 11:10

Aine


You can't.

PHPUnit only allows you to specify one bootstrap file and you need to set up everything so that each test case of each testsuite could potentially be executed and PHPUnit has no how of running "setup" code for each testsuite from a bootstrap xml file.

When using the, with phpunit 3.6 discouraged, TestSuite classes you could do it in those but my suggestion would be to just run all your generic bootstrap code in your bootstrap.php and should you need special setup for tests in app1 and in app2 to have a App1_TestCase you inherit from.

Should App1 really be a whole application I'd suggest having two separate projects with their own tests and setup code and not trying to run them in one phpunit run.

like image 29
edorian Avatar answered Oct 22 '22 13:10

edorian