[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 @ 2021-06-25 11:18  唯一客服系统开发笔记  阅读(489)  评论(0编辑  收藏  举报