13

How can I get PHPUnit to run my PHPT test cases and integrate the pass/fail status into the overall metrics? I am already aware of how to run these tests using run-phpt from the command line, but I want to run them from within PHPUnit with my other tests.

I'm aware that the PHPUnit_Extensions_PhptTestCase exists but I couldn't find any samples on how to use it.

edorian
  • 38,542
  • 15
  • 125
  • 143

2 Answers2

5

While the main point of this blog post about testing file uploads is to describe how to write PHPT tests, it shows how to integrate them into PHPUnit at the end of the post. In short, you pass the absolute path to the .phpt file to the parent constructor.

<?php
require_once 'PHPUnit/Extensions/PhptTestCase.php';

class UploadExampleTest extends PHPUnit_Extensions_PhptTestCase {
    public function __construct() {
        parent::__construct(__DIR__ . '/upload-example.phpt');
    }
}

Alternatively, you can use the PHPUnit_Extensions_PhptTestSuite class, that takes a directory as its first constructor argument and then searches for all *.phpt files within this directory.

igorw
  • 27,759
  • 5
  • 78
  • 90
David Harkness
  • 35,992
  • 10
  • 112
  • 134
  • 1
    That means that every single testcase requires a distinct hard-coded class file **and** testcase file for it? Wow, that's useless... – ircmaxell Feb 07 '11 at 18:40
  • i totally forgot about mapi s' post even so that was the reason i started caring about phpt again. Thanks ! – edorian Feb 07 '11 at 19:18
  • 6
    @ircmaxell: "Alternatively, you can use the PHPUnit_Extensions_PhptTestSuite class, that takes a directory as its first constructor argument and then searches for all *.phpt files within this directory" :) – edorian Feb 07 '11 at 19:18
  • @edorian: I had always wondered what PHPT was all about, and that post presents a great example. We have an avatar-upload controller that will be getting a unit test soon. :) – David Harkness Feb 07 '11 at 22:18
  • This is really not working for me. Is there any setup requied to get this to work? Everytime I try to run the tests (either with PhpTestCase or PhpTestSuite) the runner just breaks (not even a fail, just dies)... every time I run it phpunit complains that the class cannot be found in the file :/ Is there anything else I need other than the example above? – Matt Oct 31 '12 at 06:13
  • @DavidHarkness Yes the phpt works perfectly outside PHPUnit I'll setup my tests again and post my code. Thankyou for the reply – Matt Nov 07 '12 at 00:49
  • @DavidHarkness First of all, +1 for pointing me to gist... Awesome tool... Secondly...https://gist.github.com/4029107 I could not create directories, but the two files should be obvious which directories to put them in. I've also included output of my tests, Any help much appreciated! – Matt Nov 07 '12 at 01:58
  • @Matt - As noted in [PHPUnit #639](https://github.com/sebastianbergmann/phpunit/issues/639), this broke sometime during 3.6's development. I imagine it should be fixed relatively soon, but I posted the solution there if you want to make the change locally. – David Harkness Nov 08 '12 at 00:16
  • @DavidHarkness thankyou for the update. I'm running a older version (so its compatible with our development with zend framework 1) phpunit 3.3.17. So I guess its not the same bug. When I get some time I'll have a poke around the code :) – Matt Nov 08 '12 at 06:00
4

In your phpunit XML configuration, you can add a testsuite named PHPT (or any other name) and point to the directory where you placed the .phpt files with the suffix set to ".phpt". Phpunit will then execute all phpt-test-files from that directory:

<testsuites>
    <testsuite name="Main Test Suite">
        <directory>test</directory>
    </testsuite>
    <testsuite name="PHPT Test Suite">
        <directory suffix=".phpt">test/phpt</directory>
    </testsuite>
</testsuites>

In this example, the directory test/phpt contains the .phpt files.

Information about the format how .phpt tests are structured and written is available in depth at:

hakre
  • 193,403
  • 52
  • 435
  • 836