前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分享一个php单元测试框架PHPUnit

分享一个php单元测试框架PHPUnit

原创
作者头像
好派笔记
修改2021-10-08 14:49:56
4610
修改2021-10-08 14:49:56
举报
文章被收录于专栏:好派笔记好派笔记

开始使用 PHPUnit

下载

我们用一个 PHP Archive (PHAR) 来包含你需要使用的PHPUnit,可以从这里下载它,使其可执行,并把它放到你的 $PATH 里, 如:

➜ wget http://phar.phpunit.cn/phpunit.pha

➜ chmod +x phpunit.pha

➜ sudo mv phpunit.phar /usr/local/bin/phpunit

➜ phpunit --version

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

当然您也可以立即使用PHAR下载它,如:

➜ wget http://phar.phpunit.cn/phpunit.pha

➜ php phpunit.phar --version

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

详情请参阅文档如何 校验 PHPUnit PHAR 发行包 或 如何使用 Composer 来安装 PHPUnit。

Example代码

src/Email.php

ensureIsValidEmail($email);

$this->email = $email;

}

public static function fromString(string $email): self

{

return new self($email);

}

public function __toString(): string

{

return $this->email;

}

private function ensureIsValidEmail(string $email): void

{

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

throw new InvalidArgumentException(

sprintf(

'"%s" is not a valid email address',

$email

)

);

}

}

}

测试代码

tests/EmailTest.php

assertInstanceOf(

Email::class,

Email::fromString('user@example.com')

);

}

public function testCannotBeCreatedFromInvalidEmailAddress(): void

{

$this->expectException(InvalidArgumentException::class);

Email::fromString('invalid');

}

public function testCanBeUsedAsString(): void

{

$this->assertEquals(

'user@example.com',

Email::fromString('user@example.com')

);

}

}

如果您想理解上面所示的示例之间的差异,那么 "PHP 7 Explained 电子书可能对你有帮助.

如果您想使用示例代码 (PHP 5.6版本) ,您需要下载 http://phar.phpunit.cn/phpunit-5.6.phar 而不是 http://phar.phpunit.cn/phpunit.phar.

测试执行

➜ phpunit --bootstrap src/Email.php tests/EmailTest

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

... 3 / 3 (100%)

Time: 70 ms, Memory: 10.00MB

OK (3 tests, 3 assertions)

让我们看看上面的调用意味着的三个部分:

phpunit 调用命令行测试PHPUnit。我们假定您已下载 phpunit.phar (见上面)并已把phpunit放进你的 $PATH里.

--bootstrap src/Email.php 指示PHPUnit命令行测试在测试之前执行 include src/Email.php .

通常,但是,你使用 --bootstrap 指导PHPUnit命令行测试前包括启动脚本设置 autoloading 对于要测试的类。这样的脚本通常是使用工具来生成的,例如 Composer 或 phpab.

tests/EmailTest 指示PHPUnit命令行测试要执行的测试 EmailTest 类声明在 tests/EmailTest.php.

使用 tests 而不是 tests/EmailTest 将指示PHPUnit命令行执行所有已声明的测试 *Test.php 源代码文件在 tests 目录.

下面,您将看到测试结果的一个可选输出,它使用 --testdox 选项:

➜ phpunit --bootstrap src/Email.php --testdox tests

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

Email

[x] 可以从有效的电子邮件地址创建

[x] 无法从无效电子邮件地址创建

[x] 可以用作字符串

此输出基于这样一个概念:测试的名称可以用来记录被测试验证的行为。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档