前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHPUnit 单元测试安装与使用入门教程

PHPUnit 单元测试安装与使用入门教程

作者头像
砸漏
发布2020-11-02 15:27:12
6800
发布2020-11-02 15:27:12
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

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

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

https://phpunit.de/getting-started-with-phpunit.html

二、安装 PHPUnit

官网提供了两种方法安装

1.PHP Archive (PHAR)

代码语言:javascript
复制
➜ 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

代码语言:javascript
复制
➜ composer require --dev phpunit/phpunit ^8

➜ ./vendor/bin/phpunit --version
PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

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

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

代码语言:javascript
复制
<?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')
    );
  }
}

运行测试

代码语言:javascript
复制
➜ ./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相关内容感兴趣的读者可查看本站专题:《PHP错误与异常处理方法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》及《php优秀开发框架总结》

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、官网下载对应 PHP 版本的代码库
  • 二、安装 PHPUnit
  • 三、使用 PHPUnit 进行测试,以下代码默认你是使用Composer 安装的 PHPUnit
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档