简体   繁体   English

你如何处理 api 测试中的依赖关系?

[英]How do you handle dependency in api testing?

I have large api that I want to wirte api tests for each endpoint.我有大型 api,我想为每个端点编写 api 测试。 How should I handle testing endpoints that depend on id from other endpoints?我应该如何处理依赖于其他端点 id 的测试端点?

In our application we have to create client first then branch then products then order for that branch.在我们的应用程序中,我们必须先创建客户端,然后是分支,然后是产品,然后为该分支订购。 so if I am testing所以如果我正在测试

POST /clients
POST /branches
POST /products
POST /orders

should I write create client before every test for each endpoint?我应该在每个端点的每次测试之前编写创建客户端吗?

You can use cURL PHP for this您可以为此使用 cURL PHP

Follow : https://www.php.net/manual/pt_BR/book.curl.php关注: https ://www.php.net/manual/pt_BR/book.curl.php

Or you can run this code:或者您可以运行以下代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com"); // API url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // for get retutn requsert
curl_setopt($ch, CURLOPT_POST, 1); // for use post requsert
curl_setopt($ch, CURLOPT_POSTFIELDS, ['key'=>'value']); // for post data
$res = curl_exec($ch); // for close requset
if (curl_error($ch)) {
    var_dump(curl_error($ch)); // for print error
} else {
    print_r($res); // for print result
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM