当前位置: 首页 > news >正文

使用phpunit进行单元测试

使用phpunit进行单元测试

本教程假定您使用 PHP 8.1 或 PHP 8.2。您将学习如何编写简单的单元测试以及如何下载和运行 PHPUnit.

PHPUnit 10 的文档 在这。

下载:可以用以下2种方法之一:

1.PHP 存档 (PHAR)

我们分发了一个 PHP存档(PHAR),其中包含使用PHPUnit 10所需的一切 。只需从这里 下载 并使其可执行:

wget -O phpunit https://phar.phpunit.de/phpunit-10.phar
➜ chmod +x phpunit
➜ ./phpunit --version
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.

2.Composer

您可以使用 Composer 将 PHPUnit 作为本地、每个项目、开发时依赖项添加到您的项目中:

➜ composer require --dev phpunit/phpunit ^10./vendor/bin/phpunit --version
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.

上面显示的示例假定composer在您的$PATH上。

您的 composer.json 应该看起来像这样:

{"autoload": {"classmap": ["src/"]},"require-dev": {"phpunit/phpunit": "^10"}
}

代码

src/Email.php

<?php 
declare(strict_types=1);
final class Email
{private string $email;private function __construct(string $email){$this->ensureIsValidEmail($email);$this->email = $email;}public static function fromString(string $email): self{return new self($email);}public function asString(): 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

<?php 
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class EmailTest extends TestCase
{public function testCanBeCreatedFromValidEmail(): void{$string = 'user@example.com';$email = Email::fromString($string);$this->assertSame($string, $email->asString());}public function testCannotBeCreatedFromInvalidEmail(): void{$this->expectException(InvalidArgumentException::class);Email::fromString('invalid');}
}

测试执行:以下2种方法都可以:

1.PHP 存档 (PHAR)

./phpunit --bootstrap src/autoload.php tests
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
..                                        2 / 2 (100%)
Time: 70 ms, Memory: 10.00MB
OK (2 tests, 2 assertions)

上面假设你已经下载了phpunit.phar并将其作为phpunit放入你的$PATH,并且src/autoload.php 是一个为要测试的类设置自动加载 的脚本。这样的脚本通常使用 phpab 等工具生成。

–bootstrap src/autoload.php指示 PHPUnit 命令行测试运行程序在运行测试之前包含src/autoload.php.
tests 指示 PHPUnit 命令行测试运行程序执行在 tests 目录的 *Test.php 源代码文件中声明的所有测试.

2.Composer

./vendor/bin/phpunit tests
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
..                                        2 / 2 (100%)
Time: 70 ms, Memory: 10.00MB
OK (2 tests, 2 assertions)

上面假设 vendor/autoload.php(由 Composer 管理的自动加载器脚本)存在,并且能够加载 Email 类的代码。根据设置自动加载的方式,您可能需要立即运行composer dump-autoload。

tests 指示 PHPUnit 命令行测试运行程序执行在 tests 目录的 Test.php 源代码文件中声明的所有测试.

一些测试组件推荐:

https://packagist.org/packages/mockery/mockery
phpunit/phpunit
fakerphp/faker
https://github.com/phpstan/phpstan
vimeo/psalm
mikey179/vfsstream
rector/rector

引用

declare和strict_types

ps:declare(strict_types=1);

严格类型
默认情况下,如果能做到的话,PHP将会强迫错误类型的值转为函数期望的标量类型。例如,一个函数的一个参数期望是string,但传入的是integer,最终函数得到的将会是一个string类型的值。
可以基于每一个文件开启严格模式。在严格模式中,只有一个与类型声明完全相符的变量才会被接受,否则将会抛出一个TypeError。 唯一的一个例外是可以将integer传给一个期望float的函数。
使用 declare 语句和strict_types 声明来启用严格模式
https://blog.csdn.net/joshua317/article/details/121252625

assertsame

使用运算符检查身份
报告由 if 标识的错误,如果两个变量的类型和值不同 或者 两个变量不引用同一对象 报错
https://docs.phpunit.de/en/10.1/assertions.html#assertsame

http://www.lryc.cn/news/129162.html

相关文章:

  • MongoDB 简介
  • [LitCTF 2023]Follow me and hack me
  • Java从入门到高级的全面指南
  • linux 命令- systemctl
  • 自动驾驶,一次道阻且长的远征|数据猿直播干货分享
  • 大数据培训前景怎么样?企业需求量大吗
  • redis — 基于Spring Boot实现redis延迟队列
  • 【日常积累】Linux之init系统学习
  • Python功能制作之3D方块
  • 【0基础入门Python笔记】二、python 之逻辑运算和制流程语句
  • python中的svm:介绍和基本使用方法
  • typedef
  • 校园跑腿市场行情分析
  • 微服务相关面试题
  • 前端-ES6
  • 169. 多数元素(摩尔投票法) 题解
  • python中的cnn:介绍和基本使用方法
  • Dockerfile概念、镜像原理、制作及案例讲解
  • 07-微信小程序-注册页面-模块化
  • 考研算法第46天: 字符串转换整数 【字符串,模拟】
  • Cesium for unity 1.5.0使用注意事项
  • 初阶C语言-结构体
  • Android Studio实现解析HTML获取图片URL,将URL存到list,进行瀑布流展示
  • java学习004
  • Linux网络编程:网络基础
  • 3D沉浸式旅游网站开发案例复盘【Three.js】
  • IO的几个模型
  • 中路对线发现正在攻防演练中投毒的红队大佬
  • 【LINUX相关】生成随机数(srand、/dev/random 和 /dev/urandom )
  • spark使用心得