Using Node.js to do Function test implementation method _javascript skills

Source: Internet
Author: User
Tags assert

Info
Last week, colleagues at meeting said they were now writing function test in Java, creating a lot of redundant code, and the whole project became bloated. There is an urgent need for a simple template project to quickly build function test.

Then I went back to think, why do we have to use Java to do function test?

Node.js should be a good choice, and the JSON has a natural support, so go back to GitHub on a search, there are related projects: testosterone, so there is this blog.

Server
To do demo, naturally have to have the corresponding server to support.

Here we choose Express as the server.

First we create a server folder and create a new Package.json.

Copy Code code as follows:

{
"Name": "Wine-cellar",
"description": "Wine Cellar Application",
"Version": "0.0.1",
' Private ': true,
"Dependencies": {
"Express": "3.x"
}
}

Next Run command

Copy Code code as follows:

NPM Install

So the Express is loaded.

We implement a few simple get post methods to do experiments

Copy Code code 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 ({' Content-type ': ' Text/plain '});
Res.end ();
}, 200);
});

App.get ('/hi ', function (req, res) {
if (Req.param (' hello ')!== undefined) {
Res.writehead ({' Content-type ': ' Text/plain '});
Res.end (' hello! ');
} else {
Res.writehead ({' Content-type ': ' Text/plain '});
Res.end (' use post instead ');
}
});

App.post ('/hi ', function (req, res) {
settimeout (function () {
Res.writehead ({' 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
When the server was set up, it was natural to start testing.

The name of this project's interface is very elegant, directly on the code.

The first is to test the basic functionality

Copy Code code 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 do a simple test for the user's get post that is simulated above.

Copy Code code 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 describe each test case with give when then, you can do this:

Copy Code code as follows:

var testosterone = require (' testosterone ') ({port:3000, title: ' Test User API '})
, add = Testosterone.add
, assert = Testosterone.assert;

Testosterone
. Add (
' GIVEN a user ID to/user/{id} \ n ' +
' When it have response user \ \ n ' +
' THEN It should 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 (
' GIVEN a POST a user info to/user/edit \ n ' +
' When find modify success \ n ' +
' THEN It should 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
Through the above code, you can see that with the Java verbose HTTP header settings, and so on, testosterone really simple and elegant a lot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.