PHPUnit 单元测试安装与使用入门教程
网络编程 2021-07-04 23:05www.168986.cn编程入门
这篇文章主要介绍了PHPUnit 单元测试安装与使用,结合实例形式分析了PHPunit单元测试的下载、安装、测试运行相关操作技巧与使用注意事项,需要的朋友可以参考下
本文实例讲述了PHPUnit 单元测试安装与使用。分享给大家供大家参考,具体如下:
一、官网下载对应 PHP 版本的代码库
二、安装 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
➜ poser require --dev phpunit/phpunit ^8 ➜ ./vendor/bin/phpunit --version PHPUnit 8.0.0 by Sebastian Bergmann and contributors.
三、使用 PHPUnit 进行测试,以下代码默认你是使用 Composer 安装的 PHPUnit
安装完成后在当前目录下添加文件 EmailTest.php,文件内容如下
<?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.') ); } public function testCannotBeCreatedFromInvalidEmailAddress(): void { $this->expectException(InvalidArgumentException::class); Email::fromString('invalid'); } public function testCanBeUsedAsString(): void { $this->assertEquals( 'user@example.', Email::fromString('user@example.') ); } }
运行测试
➜ ./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程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程