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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM