Integrate PHPT test cases with PHPUnit
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_Ex开发者_如何转开发tensions_PhptTestCase
exists but I couldn't find any samples on how to use it.
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.
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:
- Creating new test files (PHP: Quality Assurance)
精彩评论