Info
Last week, my colleagues at meeting said that they are currently using java to write function test, resulting in a lot of redundant code, and the entire project has become bloated. A simple template project is urgently needed to quickly build function test.
Later I went back and thought, why do we have to use java for function test?
Node. js should be a good choice, and it has a natural support for json. So I went back to github and searched for the relevant project: testosterone, so I had this blog.
Server
To do a demo, you must have the corresponding server to support it.
Here we use Express as the server.
First, create a server folder and create package. json.
Copy codeThe Code is as follows:
{
"Name": "wine-cellar ",
"Description": "Wine Cellar Application ",
"Version": "0.0.1 ",
"Private": true,
"Dependencies ":{
"Express": "3.x"
}
}
Next run command
Copy codeThe Code is as follows:
Npm install
In this way, express is installed.
Let's implement several simple get post methods for experimentation.
Copy codeThe Code is as follows:
Var express = require ('express ')
, App = express ();
App. use (express. bodyParser ());
App. get ('/hello', function (req, res ){
Res. send ("hello world ");
});
App. get ('/', function (req, res ){
SetTimeout (function (){
Res. writeHead (200, {'content-type': 'text/plain '});
Res. end ();
},200 );
});
App. get ('/Hi', function (req, res ){
If (req. param ('hello ')! = Undefined ){
Res. writeHead (200, {'content-type': 'text/plain '});
Res. end ('Hello! ');
} Else {
Res. writeHead (500, {'content-type': 'text/plain '});
Res. end ('use post instead ');
}
});
App. post ('/Hi', function (req, res ){
SetTimeout (function (){
Res. writeHead (200, {'content-type': 'text/plain '});
Res. end (req. param ('message') | 'message ');
},100 );
});
App. get ('/user', function (req, res ){
Res. send (
[
{Name: 'jack '},
{Name: 'Tom '}
]
);
});
App. get ('/user/: id', function (req, res ){
Res. send ({
Id: 1,
Name: "node js ",
Description: "I am node js"
});
});
App. post ('/user/edit', function (req, res ){
SetTimeout (function (){
Res. send ({
Id: req. param ('id '),
Status: 1
});
},100 );
});
App. listen (3000 );
Console. log ('listening on port 3000 ...');
Testosterone
After the server is set up, you must start testing.
The interfaces of this project are named elegantly and directly written into the code.
First, test basic functions.
Copy codeThe Code is as follows:
Var testosterone = require ('testosterone') ({port: 3000 })
, Assert = testosterone. assert;
Testosterone
. Get ('/hello', function (res ){
Assert. equal (res. statusCode, 200 );
})
. Get ('/Hi', function (res ){
Assert. equal (res. statusCode, 500 );
})
. Post ('/Hi', {data: {message: 'hola '}},{
Status: 200
, Body: 'hola'
});
Then, perform a simple test on the simulated user's get post.
Copy codeThe Code is as follows:
Var testosterone = require ('testosterone') ({port: 3000 })
, Assert = testosterone. assert;
Testosterone
. Get ('/user', function (res ){
Var expectRes = [
{Name: 'jack '},
{Name: 'Tom '}
];
Assert. equal (res. statusCode, 200 );
Assert. equal (JSON. stringify (JSON. parse (res. body), JSON. stringify (expectRes ));
})
. Get ('/user/1', function (res ){
Var user = JSON. parse (res. body );
Assert. equal (res. statusCode, 200 );
Assert. equal (user. name, "node js ");
Assert. equal (user. description, "I am node js ");
})
Next, if you want to use give when then to describe each test case, you can:
Copy codeThe Code is as follows:
Var testosterone = require ('testosterone') ({port: 3000, title: 'test user api '})
, Add = testosterone. add
, Assert = testosterone. assert;
Testosterone
. Add (
'Giveen a user id to/user/{id} \ n' +
'When it have response user \ n' +
'Then it shoshould return user json ',
Function (cb ){
Testosterone. get ('/user/1', cb (function (res ){
Var expectRes = {
Id: 1,
Name: "node js ",
Description: "I am node js"
};
Assert. equal (res. statusCode, 200 );
Assert. equal (JSON. stringify (JSON. parse (res. body), JSON. stringify (expectRes ));
}));
})
. Add (
'Giveen a POST a user info to/user/edit \ n' +
'When find modify success \ n' +
'Then it shoshould resturn status 1 ',
Function (cb ){
Testosterone. post ('/user/edit', {data: {id: 1, name: "change name" }}, cb (function (res ){
Var res = JSON. parse (res. body );
Assert. equal (res. status, 1 );
}));
}
)
. Run (function (){
Require ('sys '). print ('done! ');
});
Conclusion
From the above code, we can see that testosterone, like java's lengthy http header settings, is indeed simple and elegant.