11

在 Newman 中,我想测试以确保响应代码正确、响应时间合理且响应值正确。

在某些情况下,由于网络中断或其他一些系统条件,某些请求可能会以超时或不正确的值结束,如果在几秒钟后处理相同的请求,这些将解决。

在这种情况下,我想重试相同的请求 x 次,请求之间有 Y 次超时。

如果重试后迭代通过,我希望 Newman 退出代码为 0(成功运行)。

4

3 回答 3

11

几个小时后,我以这样的功能结束:

function retryOnFailure(successCode, numberOfRetrys) {
    var key = request.name + '_counter';
    var execCounter = postman.getEnvironmentVariable(key) || 1;

    var sleepDuration = 1000;
    var waitUntilTime = new Date().getTime() + sleepDuration;
    if (responseCode.code !== successCode && execCounter <= numberOfRetrys) {
        while (new Date().getTime() < waitUntilTime) {
            // Do Nothing -> Wait
        }
        console.log('Retrying: ' + request.name + '\nGot: ' + responseCode.code + ' Expected: ' + successCode + '\nWaited: ' + sleepDuration / 1000 + 'sec  \nRetry Number: ' + execCounter + ' of ' + numberOfRetrys);
        execCounter++;
        postman.setEnvironmentVariable(key, execCounter);
        postman.setNextRequest(request.name);
    }
}

用法:

retryOnFailure(404, 4);
于 2017-04-19T13:42:21.573 回答
7

You can setup a request workflow like this:

Create a collection with a request, then:

In the pre-request tab you can implement a counter:

// Counter for number of requests
var counter = environment.counter ? _.parseInt(environment.counter) + 1 : 1;
postman.setEnvironmentVariable("counter", counter);

Your tests tab would look like this:

const code = (responseCode.code === 200);

if (code === 200 && environment.counter < X) {
    // Stop execution
    tests["Status code is 200"] = code;
    postman.setNextRequest();
}
else {
    // retry the same request
    postman.setNextRequest("Name of this request");
}

A timeout for the request itself can be configured with the newman CLI:

newman run myCollection.json --timeout-request Y
于 2017-04-19T13:19:56.010 回答
0

这是相同的可重用功能

postmanFunctions.common.retryOnFailure(predicate,retryCount,waitbetweenRetrySec,ReroutetorequestifNeeded ,postmanAssertions);
  • 谓词函数决定成败
  • 断言函数具有所有邮递员断言
  • 如果 reroute 为空,则在重试尝试后执行断言。
  • 具有重试次数和等待时间的灵活轮询(如果谓词不再通过轮询/重排)
  • 有一个 maxflow 计数器(env var),它限制流跳转的数量以避免无限循环

将以下函数存储在 Globals 或 env 中:

() => {
var sleep = (sleepDuration) => {
    var startTime = new Date().getTime();
    while (new Date().getTime() - startTime < sleepDuration) {}
}
var sleepByAsyncDelayTime = () => {
    var sleepDuration = postman.getEnvironmentVariable('asyncDelayTime') || 0;
    sleep(sleepDuration);
}
var retryOnFailure = (predicate, numberOfRetrys, sleepDuration, reRouteRequestName, postmanAssertions) => {
    var retryCountPerReq_key = request.name + '_retry_count';
    var retryCountPerReq = pm.environment.get(retryCountPerReq_key) || 0;
    var reflowCountPerReq_key = request.name + '_reflow_count';
    var reflowCountPerReq = pm.environment.get(reflowCountPerReq_key) || 0;
    var totalReflowCount_key = 'totalReflowCount';
    var totalReflowCount = pm.environment.get(totalReflowCount_key) || 0;
    var maxReflowCounter = postman.getEnvironmentVariable('maxReflowCounter') || 0;
    var maxReflowCounterPerReq = postman.getEnvironmentVariable('maxReflowCounterPerReq') || 0;

    function clearAndExit() {
        pm.environment.unset(retryCountPerReq_key);
        pm.environment.unset(reflowCountPerReq_key);
        postmanAssertions();
    }

    function retry() {
        sleep(sleepDuration);
        pm.environment.set(retryCountPerReq_key, ++retryCountPerReq);
        postman.setNextRequest(request.name);
    }

    function reFlow() {
        if (totalReflowCount < maxReflowCounter && reflowCountPerReq < maxReflowCounterPerReq) {
            pm.environment.unset(retryCountPerReq_key);
            pm.environment.set(totalReflowCount_key, ++totalReflowCount);
            pm.environment.set(reflowCountPerReq_key, ++reflowCountPerReq);
            postman.setNextRequest(reRouteRequestName);
        } else clearAndExit();
    }
    if (predicate()) clearAndExit();
    else if (retryCountPerReq < numberOfRetrys) retry();
    else if (reRouteRequestName != '') reFlow();
    else clearAndExit();
}
return {
    common: {
        sleepByAsyncDelayTime,
        sleep,
        retryOnFailure
    }
};

}

于 2020-08-19T07:15:08.660 回答