单元测试
安装
composer require --dev phpunit/phpunit
配置
项目根目录新增配置文件 phpunit.xml phpunit.xml 配置项文档 可按照项目需求进行自定义配置
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="support/bootstrap.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
shortenArraysForExportThreshold="10"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnRisky="true"
failOnWarning="true"
colors="true">
<testsuites>
<testsuite name="tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
使用
新建文件 tests/TestConfig.php
,用于测试数据库配置
<?php
use PHPUnit\Framework\TestCase;
class TestConfig extends TestCase
{public function testAppConfig()
{$config = config('app');
self::assertIsArray($config);
self::assertArrayHasKey('debug', $config);
self::assertIsBool($config['debug']);
self::assertArrayHasKey('default_timezone', $config);
self::assertIsString($config['default_timezone']);
}
}
运行
项目根目录里运行全部测试用例
./vendor/bin/phpunit
项目根目录里运行指定测试用例
./vendor/bin/phpunit tests/TestConfig.php
结果类似如下:
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.010, Memory: 6.00 MB
OK (1 test, 5 assertions)