Ping-API provide a service let you could write scripts in JavaScript or CoffeeScript to test your API.
Your test scripts will automatically run on global servers to test API, such as U.S., Japan, Germany and more...
- First test
- Initial Script
- Test Script
- Storage
- Variable API path
- Set default User-Agent
- Request Content-Type
- Multi test case
After you create the project, then you can click the New test
button to create a test
.
You can set the method, uri, headers, parameters and request body of the test.
Then you can write the test script like this.
this.testCase = function (test, response) {
test.expect(1); // how many expectations in this test case.
test.equal(response.statusCode, 200); // the response status code should be 200
test.done();
};
There is one initial script of the each project.
The initial script will be run before the each test. You can set common functions at here.
There are two section of the test case setup
and test
. The setup code should be a function and the prefix is setup
. The test code should be a function and the prefix is test
.
this.setupCaseA = function () {
/*
The setup function of CaseA. This is optional.
@return {object}
params: {object}
The url query string.
headers: {object}
body: {object|string}
If the body is object Ping-API will convert to JSON at default.
Set 'Content-Type': 'application/x-www-form-urlencoded' at headers Ping-API will convert to unl-encoded form.
*/
return {
params: {
index: 0
},
headers: {
'User-Agent': 'Chrome'
},
body: {
account: 'test-account',
password: 'password'
}
};
};
this.testCaseA = function (test, response) {
/*
The test function of CaseA.
@params test {Test}
expect: {function} arguments: num{number}
How many assertions should be passed.
equal: {function} arguments: actual{any}, expected{any}, message{string}
Test with the equal comparison operator ( === ).
done: {funnction}
Call this method when the test case was done.
@params response {Response}
statusCode: {number}
headers: {object} // all keys are lowercase
body: {string}
request: {object}
*/
test.expect(2);
test.equal(response.statusCode, 200, 'response status should be 200.');
var content = JSON.parse(response.body);
test.ok(!content.error, 'server should not return error messages.');
test.done();
};
You can use storage
to pass variable to the next test.
this.setupCase = function () {
return {
body: {
account: 'test-account',
password: 'password'
}
};
};
this.testCase = function (test, response) {
test.equal(response.statusCode, 200);
var content = JSON.parse(response.body);
test.ok(content.token);
storage.token = content.token;
test.done();
};
this.setupCase = function () {
return {
params: {
token: storage.token
}
};
};
this.testCase = function (test, response) {
test.equal(JSON.parse(response.body), {
account: 'test-account',
id: 1
});
test.done();
};
this.setupCase = function () {
return {
params: {
userId: '550e8400-e29b-41d4-a716-446655440000'
}
};
};
this.testCase = function (test) {
test.done();
};
All of test requests will append this user-agent at header.
storage.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) Chrome/46.0.2490.80'
};
Ping-API provides application/x-www-form-urlencoded
and application/json
of request content type.
// application/x-www-form-urlencoded request
this.setupCase = function () {
return {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: {
account: 'test-account',
password: 'password'
}
};
};
this.testCase = function (test, response) {
test.equal(response.statusCode, 200);
test.done();
};
// application/json request
this.setupCase = function () {
return {
body: {
account: 'test-account',
password: 'password'
}
};
};
this.testCase = function (test, response) {
test.equal(response.statusCode, 200);
test.done();
};
You could write multi test case at one uri.
this.setupSuccessCase = function () {
return {
body: {
account: 'test-account',
password: 'password'
}
};
};
this.testSuccessCase = function (test, response) {
test.equal(response.statusCode, 200);
test.done();
};
this.setupFailureCase = function () {
return {
body: {
account: 'not-exist-account',
password: 'password'
}
};
};
this.testFailureCase = function (test, response) {
test.equal(response.statusCode, 400);
test.done();
};