PHPUnit-入门
这是PHPUnit的入门教程。
什么是PHPUnit?
PHPUnit是一个单元测试框架,由Sebastian Bergmann创建,其开发托管在GitHub上。
先决条件
为了学习PHPUnit,假定您熟悉PHP和PHP中的面向对象编程。
需要的东西
以下是遵循本教程所需的事项列表。
您机器上的PHP开发环境。
Composer-这是PHP的依赖项管理器
文本编辑器编写代码。
例如:SublimeText,Notepad ++,TextMate,Coda等您也可以使用IDE(例如Eclipse,NetBeans,PhpStorm或者任何其他PHP的IDE)
可选的
- 像MySQL这样的数据库
- Web浏览器,例如Chrome,Firefox
让我们开始吧。
建立项目
让我们开始旅程,首先在本地主机中创建一个项目文件夹,并将其命名为phpunit。
现在移至该项目文件夹,并使用composer要求phpunit。
Composer是一个PHP依赖项管理器。
在终端使用下面的命令来获取PHPUnit的在您的项目。
$composer require phpunit/phpunit
theitroad-MacBook-Pro:phpunit theitroadtheitroad$composer require phpunit/phpunit Using version ^5.7 for phpunit/phpunit ./composer.json has been created Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 25 installs, 0 updates, 0 removals - Installing symfony/yaml (v3.2.7) Downloading: 100% - Installing sebastian/version (2.0.1) Loading from cache - Installing sebastian/resource-operations (1.0.0) Loading from cache - Installing sebastian/recursion-context (2.0.0) Loading from cache - Installing sebastian/object-enumerator (2.0.1) Loading from cache - Installing sebastian/global-state (1.1.1) Loading from cache - Installing sebastian/exporter (2.0.0) Loading from cache - Installing sebastian/environment (2.0.0) Loading from cache - Installing sebastian/diff (1.4.1) Loading from cache - Installing sebastian/comparator (1.2.4) Loading from cache - Installing doctrine/instantiator (1.0.5) Loading from cache - Installing phpunit/php-text-template (1.2.1) Loading from cache - Installing phpunit/phpunit-mock-objects (3.4.3) Loading from cache - Installing phpunit/php-timer (1.0.9) Loading from cache - Installing phpunit/php-file-iterator (1.4.2) Loading from cache - Installing sebastian/code-unit-reverse-lookup (1.0.1) Loading from cache - Installing phpunit/php-token-stream (1.4.11) Loading from cache - Installing phpunit/php-code-coverage (4.0.8) Downloading: 100% - Installing webmozart/assert (1.2.0) Loading from cache - Installing phpdocumentor/reflection-common (1.0) Loading from cache - Installing phpdocumentor/type-resolver (0.2.1) Loading from cache - Installing phpdocumentor/reflection-docblock (3.1.1) Loading from cache - Installing phpspec/prophecy (v1.7.0) Loading from cache - Installing myclabs/deep-copy (1.6.1) Downloading: 100% - Installing phpunit/phpunit (5.7.19) Downloading: 100% symfony/yaml suggests installing symfony/console (For validating YAML files using the lint command) sebastian/global-state suggests installing ext-uopz (*) phpunit/phpunit suggests installing phpunit/php-invoker (~1.1) Writing lock file Generating autoload files theitroad-MacBook-Pro:phpunit theitroadtheitroad$
安装作曲家的项目文件夹看起来像下面这样。
运行phpunit
打开终端并运行以下命令以检查phpunit是否正常运行。
$./vendor/bin/phpunit
theitroad-MacBook-Pro:phpunit theitroadtheitroad$./vendor/bin/phpunit PHPUnit 5.7.19 by Sebastian Bergmann and contributors.
终端中将打印更多行。
我已经跳过了这些行。
如果您能够得到上面写着PHPUnit版本和作者的行,则表明已安装。
创建一个测试文件夹
现在,在我们的项目文件夹中,我们将继续创建一个测试文件夹。
这将保存我们所有的测试文件。
因此,在终端中运行以下命令以创建测试文件夹。
$mkdir test
创建一个应用程序文件夹
现在,在项目文件夹中,我们将创建一个应用程序文件夹。
在此文件夹中,我们将保留所有应用程序代码。
$mkdir app
设置composer自动加载
打开composer.json文件并添加以下行。
{ "require": { "phpunit/phpunit": "^5.7" }, "autoload": { "psr-4": { "App\": "app" } } }
我们添加了autoload
部分,这意味着我们要从app
目录中加载代码。
现在我们将在终端中运行dump-autoload
命令来更新composer的自动加载器。
$composer dump-autoload -o
theitroad-MacBook-Pro:phpunit theitroadtheitroad$composer dump-autoload -o Generating optimized autoload files theitroad-MacBook-Pro:phpunit theitroadtheitroad$
创建phpunit.xml文件
现在,我们将配置phpunit从给定位置为我们运行测试。
因此,我们将在项目文件夹中创建一个phpunit.xml文件,并其中编写以下代码。
<?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="vendor/autoload.php" stopOnFailure="true" colors="true" verbose="true"> <testsuites> <testsuite name="My Test Suite"> <directory>test</directory> </testsuite> </testsuites> </phpunit>
因此,我们为PHPUnit定义了一些配置。
我们正在引导vender/autoload.php
文件。
这将在我们的测试运行之前加载。
我们想在失败时停止PHPUnit执行。
我们希望PHPUnit使用颜色突出显示输出。
我们希望从PHPUnit获得详细的输出。
我们还定义了一个测试套件并将其命名为"我的测试套件",我们希望从测试目录运行测试。
运行phpunit测试
现在,如果我们运行phpunit测试,我们将获得以下输出。