首页 > Web开发 > 详细

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

时间:2021-06-25 13:22:52      阅读:6      评论:0      收藏:0      [点我收藏+]

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);
    }
}

 

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

原文:https://www.cnblogs.com/taoshihan/p/14930109.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!