Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of test cases execution in PHPUnit

Tags:

php

phpunit

I have defined the list of the Test files which needs to be executed in a order in phpunit.xml.

As per http://phpunit.de/manual/3.7/en/organizing-tests.html#organizing-tests.xml-configuration it is mentioned that order of the test case can be defined as:

<phpunit>
  <testsuites>
    <testsuite name="Object_Freezer">
      <file>Tests/Freezer/HashGenerator/NonRecursiveSHA1Test.php</file>
      <file>Tests/Freezer/IdGenerator/UUIDTest.php</file>
      <file>Tests/Freezer/UtilTest.php</file>
      <file>Tests/FreezerTest.php</file>
      <file>Tests/Freezer/StorageTest.php</file>
      <file>Tests/Freezer/Storage/CouchDB/WithLazyLoadTest.php</file>
      <file>Tests/Freezer/Storage/CouchDB/WithoutLazyLoadTest.php</file>
    </testsuite>
  </testsuites>
</phpunit>

But as it seems like PHPUnit is ignoring the config in phpunit.xml and executing the test cases in alphabetical order.

I want to define order just because I want one of my test case to be executed at the end. Below is my code:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit 
    colors="true" 
    stopOnFailure="false" 
    forceCoversAnnotation="true"
    bootstrap="../application/third_party/CIUnit/bootstrap_phpunit.php">
    <testsuites>
        <testsuite name="ModelTests">
            <directory suffix=".php">models</directory>
            <file>./models/aa.php</file>
            <file>./models/bb.php</file>
            <file>./models/cc.php</file>
            <file>./models/dd.php</file>
            <file>./models/ab.php</file>
            <file>./models/ac.php</file>
        </testsuite>
    </testsuites>
    <filter>
    <whitelist addUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">models</directory>
    </whitelist>
    </filter>
</phpunit>

when I execute phpunit it prints: Configuration read from /var/www/builds/latest/tests/phpunit.xml

Any idea?

like image 922
DShah Avatar asked Apr 03 '14 10:04

DShah


1 Answers

You need to tell phpunit to use the test suite so that it is running the tests in the proper order. I am guessing that you are just using phpunit . or something similar to run the tests. In this case, PHPUnit loads up the tests and runs them in a pretty random fashion. In order to use the order that you are specifying you need to do phpunit --testsuite ModelTest This tells PHPUnit to use the order that are specifying in the suite.

However, it also will only run the tests listed in the XML there, so make sure that your list is comprehensive.

Also, as mentioned in the linked answer in the comments, having your tests be required to be run in a certain order isn't an ideal situation. You should try to either change your design or your tests so that you are not dependent on them being run in a certain order.

like image 167
Schleis Avatar answered Oct 05 '22 12:10

Schleis