php单元测试工具入门,PHPUnit 单元测试安装与使用入门教程

本文实例讲述了PHPUnit 单元测试安装与使用。分享给大家供大家参考,具体如下:

一、官网下载对应 PHP 版本的代码库

3573bd4418495ee3dd5c5bc6574fd264.png

二、安装 PHPUnit

官网提供了两种方法安装

1. PHP Archive (PHAR)

➜ wget -O phpunit https://phar.phpunit.de/phpunit-8.phar

➜ chmod +x phpunit

➜ ./phpunit --version

PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

2. Composer

➜ composer require --dev phpunit/phpunit ^8

➜ ./vendor/bin/phpunit --version

PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

三、使用 PHPUnit 进行测试,以下代码默认你是使用 Composer 安装的 PHPUnit

安装完成后在当前目录下添加文件 EmailTest.php,文件内容如下

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class EmailTest extends TestCase

{

public function testCanBeCreatedFromValidEmailAddress(): void

{

$this->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')

);

}

}

运行测试

➜ ./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/EmailTest

PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

... 3 / 3 (100%)

Time: 70 ms, Memory: 10.00MB

OK (3 tests, 3 assertions)

希望本文所述对大家PHP程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值