By extending and customizing the PHPUnit framework, the problem that the original framework cannot meet the needs can be solved. In terms of extension, it includes custom assertions, Matcher and DataProvider; in terms of customization, it involves creating custom runners and overriding Bootstrapper. In practical applications, extended assertions can verify special characters, custom Matchers can verify list elements, and overriding Bootstrapper can increase the execution timeout limit.
Extension and customization of PHP unit testing framework
Unit testing can significantly improve code quality, but the native framework may not be able to meet the requirements All needs. Extending and customizing the framework solves this problem.
Extension methods
1. Custom assertion
PHPUnit provides assertion methods, but sometimes custom assertions are required. Create a new assertion method using the Assert
class:
class CustomAssertions extends PHPUnit_Framework_Assert { public static function assertTrueWithMessage($condition, string $message) { self::assertTrue($condition, $message); } }
2. Custom Matcher
Matcher validates whether a value meets specific conditions. Create a custom Matcher using the prophesize
library:
class CustomMatcher { public function isEven($value) { return $value % 2 == 0; } } $propecy = $prophesize(new CustomMatcher()); $propecy->isEven(6)->shouldBeTrue();
3. Implement DataProvider
DataProvider provides a custom source for test data. Use PHPUnit_Extensions_DataProvider_ArrayDataProvider
to create a custom DataProvider:
class CustomDataProvider { public static function provideData() { return [ ['foo', 'bar'], ['baz', 'qux'] ]; } } $dataProvider = new PHPUnit_Extensions_DataProvider_ArrayDataProvider(CustomDataProvider::provideData());
Customized framework
1. Create a custom runner
The runner is responsible for executing tests. Create a custom runner using PHPUnit_Framework_TestSuite_DataProvider
:
class CustomTestRunner extends PHPUnit_Framework_TestSuite_DataProvider { protected function setUp(): void { // 自定义设置 } protected function tearDown(): void { // 自定义清理 } }
2. Override Bootstrapper
Bootstrapper Set up the test running environment before the test runs. Use PHPUnit_Util_Configuration
to override Bootstrapper:
class CustomBootstrapper { public static function bootstrap() { // 自定义引导 PHPUnit_Util_Configuration::$defaultEnforceTimeLimit = 300; } } PHPUnit_Util_Configuration::$bootstrap = 'CustomBootstrapper::bootstrap';
Practical case
##Extended assertion:Verify the existence of special characters:
CustomAssertions::assertTrueWithMessage( strpos($string, "\t") !== false, "String does not contain a tab character" );
Custom Matcher:Verify that list contains elements:
$prophesize(new CustomMatcher())->contains(['foo', 'bar'])->shouldBeTrue();
Override Bootstrapper:Increase execution timeout limit to 300 seconds:
CustomBootstrapper::bootstrap();
The above is the detailed content of Extension and customization of PHP unit testing framework. For more information, please follow other related articles on the PHP Chinese website!