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.
PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.
PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. PHPUnit 9 is the current stable version. PHPUnit 10 is currently in development.
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.
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:
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