对于我的 API 测试,我的代码以不同的方式表现。我使用带有节点 js 的 jasmine 框架。我的 GET 请求将给出 2 个响应
- 成功 json,带有 200 状态码。响应时间为 400-500 毫秒
- 失败json,带200个状态码(有效失败),响应时间大于10000ms
在第一种情况下,我的测试用例的代码如下
describe('Verification of BS_004_addressCheck',()=>{
it('Verify success response for BS_004_addressCheck',function(done){
var path=require('path');
let endpoint=require(path.resolve('./config/endpoint_BS_004.json'));
// let pincodes=require(path.resolve('./config/pincodes.json'));
//let request=require(path.resolve('./config/postPin.json'));
//console.log(request.id,request.Name);
const fetch=require('node-fetch');
let baseUrl=endpoint.url;
let apikey=endpoint.apikey;
let country=endpoint.country;
let postcode=endpoint.postcode;
let picklistcount=endpoint.picklistcount;
let uniqueAddressReference=endpoint.uniqueAddressReference;
let fullUrlWithQueryParameters= baseUrl + "?apikey=" + apikey + "&country=" + country + "&postcode=" + postcode + "&picklistcount=" + picklistcount + "&uniqueAddressReference=" + uniqueAddressReference
//let pinCode=pincodes.Vlcy;
console.log(fullUrlWithQueryParameters);
console.log("test");
getR(fullUrlWithQueryParameters)
.then(jsonRes=>{
console.log(jsonRes);
expect(jsonRes).not.toBeUndefined();
expect(jsonRes.Header.ActivityStatusEnum).toBe('SUCCESS');
})
.then(done)
})
})
我的第二种情况的代码与上面的相同。但是在我的第一种情况下,我得到了通过的规范,在第二种情况下,我得到了以下失败
1) Verification of BS_004_addressCheck Verify success response for BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
1 spec, 1 failure
Finished in 5.012 seconds
Randomized with seed 21049 (jasmine --random=true --seed=21049)
我的成功响应将花费近 400 毫秒,但我的失败响应将花费近 10000 毫秒。所以我猜测并添加了茉莉花超时如下(见上面的块)
describe('Verification of BS_004_addressCheck',()=>{
beforeAll(function(done) {
jasmine.DEFAULT_TIMEOUT_INTERVAL= 10000;
});
//jasmine.DEFAULT_TIMEOUT_INTERVAL= 10000;
it('Verify success response for BS_004_addressCheck',function(done){
var path=require('path');
let endpoint=require(path.resolve('./config/endpoint_BS_004.json'));
// let pincodes=require(path.resolve('./config/pincodes.json'));
//let request=require(path.resolve('./config/postPin.json'));
//console.log(request.id,request.Name);
const fetch=require('node-fetch');
let baseUrl=endpoint.url;
let apikey=endpoint.apikey;
let country=endpoint.country;
let postcode=endpoint.postcode;
let picklistcount=endpoint.picklistcount;
let uniqueAddressReference=endpoint.uniqueAddressReference;
let fullUrlWithQueryParameters= baseUrl + "?apikey=" + apikey + "&country=" + country + "&postcode=" + postcode + "&picklistcount=" + picklistcount + "&uniqueAddressReference=" + uniqueAddressReference
//let pinCode=pincodes.Vlcy;
console.log(fullUrlWithQueryParameters);
console.log("test");
getR(fullUrlWithQueryParameters)
.then(jsonRes=>{
console.log(jsonRes);
expect(jsonRes).not.toBeUndefined();
expect(jsonRes.Header.ActivityStatusEnum).toBe('SUCCESS');
})
.then(done)
})
})
但现在我有2个错误..
Failures:
1) Verification of BS_004_addressCheck Verify success response for BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Suite error: Verification of BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
1 spec, 2 failures
Finished in 15.015 seconds
Randomized with seed 21999 (jasmine --random=true --seed=21999).
我不知道如何解决这个问题。请有人帮助我。我调用的函数 getR 如下
let getR = (url) => {
console.log('test');
//console.log(url);
//console.log(id);
return fetch(url, {method: 'GET', agent: new HttpsProxyAgent('http://10.10.104.4:50683')})
.then(resRaw =>{
console.log(resRaw);
return resRaw.text();
})
.then(resJson=>{
console.log(resJson);
let res=JSON.parse(resJson);
//return res;
console.log(res.Header.ActivityStatusEnum);
return res;
})
//.then(validateResponse);
};
global.get=get;
global.getR=getR;
我认为这个问题是因为响应时间长。但不知道如何解决这个问题。请指教。