PHPUnit单元测试如何安装与使用-创新互联

这篇文章主要讲解了“PHPUnit 单元测试如何安装与使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PHPUnit 单元测试如何安装与使用”吧!

创新互联公司专注于白城企业网站建设,成都响应式网站建设,成都做商城网站。白城网站建设公司,为白城等地区提供建站服务。全流程按需制作网站,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

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

PHPUnit 单元测试如何安装与使用

二、安装 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,文件内容如下

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)

感谢各位的阅读,以上就是“PHPUnit 单元测试如何安装与使用”的内容了,经过本文的学习后,相信大家对PHPUnit 单元测试如何安装与使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!


当前题目:PHPUnit单元测试如何安装与使用-创新互联
URL地址:http://myzitong.com/article/dhihoi.html