首页 > 其他分享 >laravel:单元测试之http测试(10.27.0)

laravel:单元测试之http测试(10.27.0)

时间:2023-10-23 12:45:12浏览次数:28  
标签:laravel Tests http 单元测试 test NewsTest php response

一,相关文档:

https://learnku.com/docs/laravel/10.x/http-tests/14896

二,php代码:

1,创建test程序

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan make:test NewsTest

   INFO  Test [tests/Feature/NewsTest.php] created successfully.

2,代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <?php   namespace Tests\Feature;   use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase;   class NewsTest extends TestCase {     /**      * A basic feature test example.      */     public function test_example(): void     {         //年龄超过100         $response = $this->get('/news/home?name=liu&age=123');         $response->assertStatus(200)->assertJson(['code'=>500]);         //年龄小于18         $response = $this->get('/news/home?name=liu&age=5');         $response->assertStatus(200)->assertJson(['code'=>500]);         //年龄在范围内         $response = $this->get('/news/home?name=liu&age=80');         $response->assertStatus(200)->assertJson(['code'=>10]);     } }

三,测试效果

1,通过artisan运行

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan test

   PASS  Tests\Unit\ExampleTest
  ✓ that true is true

   PASS  Tests\Feature\ExampleTest
  ✓ the application returns a successful response                                                                                                                        0.10s  

   PASS  Tests\Feature\NewsTest
  ✓ example                                                                                                                                                              0.03s  

  Tests:    3 passed (8 assertions)
  Duration: 0.17s

2,也可以用phpunit来指定运行测试类

liuhongdi@lhdpc:/data/laravel/dignews$ ./vendor/bin/phpunit tests/Feature/NewsTest.php 
PHPUnit 10.4.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.1
Configuration: /data/laravel/dignews/phpunit.xml

.                                                                   1 / 1 (100%)

Time: 00:00.111, Memory: 24.00 MB

OK (1 test, 6 assertions)

四,查看laravel框架的版本:

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0

标签:laravel,Tests,http,单元测试,test,NewsTest,php,response
From: https://www.cnblogs.com/architectforest/p/17782149.html

相关文章

  • 老大加需求:做一个支持超大文件 HTTP 断点续传的上传服务,我懵逼了~
    作者:大飞飞鱼来源:blog.csdn.net/ababab12345/article/details/80490621Part1前言最近由于笔者所在的研发集团产品需要,需要支持高性能的大文件(大都数是4GB以上)的http上传,并且要求支持http断点续传。笔者在以前的博客如何实现支持大文件的高性能HTTP文件上传服务器已经介绍了......
  • 前端接口请求HTTP设置自定义header属性字段大小写问题
    问题:接口请求头传token值的字段为tokenValue,需要用到token的接口一直不能成功请求。后端排查发现没有接收到token,前端虽然传了token值,但是发现浏览器把tokenValue变成了Tokenvalue,导致后端没正确接收到token值。原因是:HTTPRFC里规定,大小写不敏感。HTTP/1.x大小写不敏感,但现实是......
  • laravel:编写命令行脚本(10.27.0)
     一,相关文档https://learnku.com/docs/laravel/10.x/artisan/14859二,php代码1,创建commandliuhongdi@lhdpc:/data/laravel/dignews$phpartisanmake:commandCart   INFO  Consolecommand[app/Console/Commands/Cart.php]createdsuccessfully.2,command的代......
  • laravel:路由组(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/routing/14845二,查看所有注册的路由1,查看路由:liuhongdi@lhdpc:/data/laravel/dignews$phpartisanroute:listGET|HEAD/......................................................................................
  • laravel:blade模板(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/blade/14852二,创建controller和view1,创建controllerliuhongdi@lhdpc:/data/laravel/dignews$phpartisanmake:controllerCommentController   INFO  Controller[app/Http/Controllers/CommentController.php......
  • Linux编译安装 drogon(高性能http服务器)
    实际上还是建议用Ubuntu进行编译,要方便的多drogon编译安装:https://zhuanlan.zhihu.com/p/601632372drogon编译安装:https://wenku.baidu.com/view/4408ed4e84c24028915f804d2b160b4e777f8150.html一、编译前置依赖项目jsoncpp#项目地址:https://github.com/open-source-pars......
  • Https 安全协议版本支持检测工具
    需求:帮朋友做了一个小工具,主要目的是检查局域网(在线检查网站无法访问内网)的https站点配置的安全协议版本,方便给第三方提供接口文档,描述https站点所使用的安全协议。相信大家在开发过程中,可能会遇到“Therequestwasaborted:CouldnotcreateSSL/TLSsecurechannel”异常,......
  • HTTPS 的加密流程
    HTTPS是在HTTP的基础上进行了一层加密,加密就是把明文(要传输的信息)进行一系列变换,生成密文。解密就是把密文再进行一系列变换,还原成明文。在这个加密和解密的过程中,往往需要一个或者多个中间的数据,辅助进行这个过程,这样的数据称为密钥。HTTPS的工作过程既然要......
  • laravel:服务容器(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/container/14842二,php代码:假设我们有两种商品:虚拟商品如账号,实体商品如手办需要销售1,App\extend\mall\GoodsInterface.php1234567<?phpnamespaceApp\extend\mall;//接口interfaceGoodsInterfa......
  • laravel:服务提供者(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/providers/14843二,php代码:1,业务代码:App\extend\mall\GoodsInterface.php1234567<?phpnamespaceApp\extend\mall;//接口interfaceGoodsInterface{    publicfunctionsale();}......