gpt4 book ai didi

php - 让 Laravel/PHPUnit 记住所有完成的请求

转载 作者:搜寻专家 更新时间:2023-10-31 21:22:22 25 4
gpt4 key购买 nike

我使用 Laravel 和 PHPUnit。在我的测试中,我有很多请求倾向于测试我的类似 REST 的 API,并且它做得很好。现在我想知道我是否可以使用所有这些测试来为我的 API 做一些描述/文档。

正式地,我如何记录/访问由 $this->get(...) 创建的所有请求和响应, $this->post(...)等等?

最佳答案

根据您编写测试名称的方式,您可以使用 PHPUnit cli 上的选项。您可以阅读更多关于它们的信息 here .

--testdox-html Write agile documentation in HTML format to file.
--testdox-text Write agile documentation in Text format to file.

当您运行测试时,而不仅仅是运行:

phpunit 

尝试:

phpunit --testdox-html documentation.html

再详细一点description :

PHPUnit's TestDox functionality looks at a test class and all the test method names and converts them from camel case PHP names to sentences: testBalanceIsInitiallyZero() becomes "Balance is initially zero". If there are several test methods whose names only differ in a suffix of one or more digits, such as testBalanceCannotBecomeNegative() and testBalanceCannotBecomeNegative2(), the sentence "Balance cannot become negative" will appear only once, assuming that all of these tests succeed.

输出如下所示:

phpunit --testdox BankAccountTest
PHPUnit 6.1.0 by Sebastian Bergmann and contributors.

BankAccount
[x] Balance is initially zero
[x] Balance cannot become negative

更新

就记录所有请求而言,唯一想到的就是扩展测试用例。我自己尝试了一个简单的形式,它似乎有效:

abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{

public function get($uri, array $headers = [])
{
parent::get($uri, $headers);
$txt = "GET REQUEST: ".$uri." HEADERS: ".json_encode($headers)."\n";
file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
}

}

关于php - 让 Laravel/PHPUnit 记住所有完成的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44032095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com