laravel:单元测试之http测试(10.27.0)
一,相关文档:
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]); } } |
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/23/laravel-dan-yuan-ce-shi-zhi-http-ce-shi/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
三,测试效果
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