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

hyperf 十二、自动化测试

文档教程:Hyperf

co-phpunit提供测试,在composer中测试。

 "scripts": {"test": "co-phpunit --prepend test/bootstrap.php -c phpunit.xml --colors=always",
}

测试中使用Hyperf\Testing\Client模拟请求,该类调用Hyperf\HttpServer\Server中的request()方法获取请求数据。

#HyperfTest\HttpTestCase
use Hyperf\Testing\Client;
use PHPUnit\Framework\TestCase;abstract class HttpTestCase extends TestCase
{/*** @var Client*/protected $client;public function __construct($name = null, array $data = [], $dataName = ''){parent::__construct($name, $data, $dataName);$this->client = make(Client::class);}
public function __call($name, $arguments){return $this->client->{$name}(...$arguments);}
}#HyperfTest\Cases\ExampleTest
class ExampleTest extends HttpTestCase
{
}#Hyperf\Testing\Client
use Hyperf\HttpServer\Server;
class Client extends Server
{protected $baseUri = 'http://127.0.0.1/';public function __construct(ContainerInterface $container, PackerInterface $packer = null, $server = 'http'){parent::__construct($container, $container->get(HttpDispatcher::class), $container->get(ExceptionHandlerDispatcher::class), $container->get(ResponseEmitter::class));$this->packer = $packer ?? new JsonPacker();$this->initCoreMiddleware($server);$this->initBaseUri($server);}public function get($uri, $data = [], $headers = []){$response = $this->request('GET', $uri, ['headers' => $headers,'query' => $data,]);return $this->packer->unpack((string) $response->getBody());}
}

相关方法:

Hyperf\Testing\Client::get($uri, $data = [], $headers = [])

Hyperf\Testing\Client::post($uri, $data = [], $headers = [])

Hyperf\Testing\Client::put($uri, $data = [], $headers = [])

Hyperf\Testing\Client::delete($uri, $data = [], $headers = [])

Hyperf\Testing\Client::json($uri, $data = [], $headers = [])

Hyperf\Testing\Client::file($uri, $data = [], $headers = [])

Hyperf\Testing\Client::request(string $method, string $path, array $options = [])

Hyperf\Testing\Client::initRequest(string $method, string $path, array $options = [])

Hyperf\Testing\Client::sendRequest(ServerRequestInterface $psr7Request)

具体断言函数参照phpunit。

一、编写测试

namespace HyperfTest\Cases;use HyperfTest\HttpTestCase;
/*** @internal* @coversNothing*/
class ExampleTest extends HttpTestCase
{public function testExample(){$this->assertTrue(true);//var_dump($this->get('/'));$this->assertTrue(is_array($this->get('/')));}
}
composer test -- --filter=test

因为co-phpunit脱胎于phpunit,所以filter其实也是phpunit选项。

二、测试替身

测试替身文章原文:PHPUnit 手册 – 第 9 章 测试替身

测试替身用mockery实现。

mockery:

composer地址:mockery/mockery - Packagist

文档地址:Mockery — Mockery Docs 1.0-alpha documentation

github地址:https://github.com/mockery/mockery

 创建的类名必须设置名称以Test结尾,否则不会调用。

#./phpunit.xml 限制后缀内容
<testsuites><testsuite name="Tests"><directory suffix="Test.php">./test</directory></testsuite>
</testsuites># composer 命令配置
"scripts": {"test": "co-phpunit --prepend test/bootstrap.php -c phpunit.xml --colors=always",
}
declare (strict_types = 1);
namespace HyperfTest\Cases;use App\Api\TestApi;
use App\Service\TestingService;
use HyperfTest\HttpTestCase;
use Hyperf\Di\Container;
use Mockery;/*** @internal* @coversNothing*/
class Example2Test extends HttpTestCase
{protected function tearDown(): void{Mockery::close();}public function test2(){$container = $this->getContainer();$res = $container->get(TestingService::class)->test();$this->assertEquals(1, $res['status']);}/*** @return Container*/protected function getContainer(){$container = Mockery::mock(Container::class);$apiStub = $this->createMock(TestApi::class);$apiStub->method('test')->willReturn(['status' => 1,]);//旧版//$container->shouldReceive('get')->with(TestingService::class)->andReturn(new TestingService($apiStub));//新版$container->allows()->get(TestingService::class)->andReturn(new TestingService($apiStub));return $container;}
}
http://www.lryc.cn/news/109103.html

相关文章:

  • dblink简单使用
  • Typescript 第十一章 与JavaScript互操作(外参变量声明,外参类型声明,外参模块声明)
  • 从0到1框架搭建,Python+Pytest+Allure+Git+Jenkins接口自动化框架(超细整理)
  • 在windows配置redis的一些错误及解决方案
  • 真机搭建中小网络
  • Linux:shell脚本:基础使用(1)
  • carla中lka实现(一)
  • 常见的数据结构(顺序表、顺序表、链表、栈、队列、二叉树)
  • (12)理解委托,反射,Type,EvenInfo,插件, 组合枚举,BindingFlags,扩展方法及重载,XML认识
  • 软件建设方案技术方案实施方案密码评测方案等保测评方案人员培训方案项目建设与运行管理项目招标方案模板目录
  • pytorch中torch.einsum函数的详细计算过程图解
  • 【iOS】App仿写--天气预报
  • 快速远程桌面控制公司电脑远程办公
  • 亚信科技AntDB数据库专家出席数据库标准研讨会并参与研讨
  • 【我们一起60天准备考研算法面试(大全)-第三十四天 34/60】【前缀和】【北邮】
  • 【数据分析】numpy (二)
  • Vue3小案例—v-model 双向数据绑定实现动态列表增加和删除
  • MySQL 重置root 密码
  • OpenCV图像处理技巧之空间滤波
  • Java超级玛丽小游戏制作过程讲解 第一天 创建窗口
  • 【POP3/IMAP/SMTP】QQ邮箱设置
  • 云计算——常见集群策略
  • c语言locale.h简介
  • C++运算符重载详解(赋值、流插入流提取、前置后置++、取地址)
  • sql的count函数优化
  • Ai创作系统ChatGPT源码搭建教程+附源码
  • 力扣 416. 分割等和子集
  • sqlyog导出mysql数据字典
  • 【C++】多态的实现及其底层原理
  • 【网络编程】TCP带外数据总结