[PHP] hyperf单元测试模拟http请求

hyperf框架自带单元测试工具

安装完框架后

composer create-project hyperf/hyperf-skeleton

直接在test/Cases下编写单元测试代码

 

 

 

比如我的两个接口一个是 /   , 一个是 /hello , 返回的必须都是json信息才可以,直接返回字符串,测试框架get方法得到的是null

在不启动服务的情况下就可以进行测试,也可以看到打印信息

测试/hello 接口

composer test --  --filter=testHello

 

 

复制代码
<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace HyperfTest\Cases;

use HyperfTest\HttpTestCase;

/**
 * @internal
 * @coversNothing
 */
class ExampleTest extends HttpTestCase
{
    public function testExample()
    {
        $this->assertTrue(true);
        $res=$this->get('/');
        var_dump($res);
        $this->assertTrue(is_array($res));
    }
    public function testHello()
    {
        $res=$this->get("/hello");
        var_dump($res);
        $this->assertSame(["hello","tsh"],$res);
    }
}
复制代码

 

posted @   唯一客服系统开发笔记  阅读(571)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-06-25 [android] socket在手机上的应用
2016-06-25 [android] 网络链接类型和渠道
2016-06-25 [android] android通信协议
点击右上角即可分享
微信分享提示
1
chat with us