You made a change. Did it break anything else? Not sure, unless you have automated testing.
Have you ever fixed one thing in your software only to find that you broke something else in the process? No? Me neither. So this article is for those "other people." OK, if I'm completely honest, I have bugs and I don't do a good job of automating my integration tests.
What are integration tests, how does one automate them, and how do they differ from “unit” tests? Good question. A unit test is a small bit of code that tests an API to make sure it functions as expected. An integration test focuses on a bigger-picture test and usually span across multiple API calls. This article will lean more toward integration testing.
If you haven't heard yet, IBM has open-sourced the Node.js iToolkit per this RFE (you need to register to access this site). This is an important step in the IBM i side of IBM embracing open source because it would be silly to pursue open source and then not allow community collaboration and contribution in order to follow in its footsteps of success. The source for the Node.js iToolkit can be found here. One of the first things I noticed about the project is it lacked any form of automating testing, so I logged an issue. The IBM Node.js team is quick to act, and they had some foundational integration tests in place in short order. Let's walk through the steps of obtaining the code, running the tests, and adding our own test.
I visited the Node.js iToolkit repo to obtain the below git clone command. Enter that command in a PASE shell and hit Enter.
% git clone
Go into the newly created directory.
% cd nodejs-itoolkit
I am using the zsh shell, and it conveys my current directory as part of the prompt, as shown below. Use the ls command to learn the contents of the nodejs-itoolkit directory.
nodejs-itoolkit% ls -al
total 128
drwxr-sr-x 5 usr2l6ru 0 8192 Feb 28 16:35 .
drwxr-sr-x 9 usr2l6ru 0 8192 Feb 28 16:35 ..
drwxr-sr-x 8 usr2l6ru 0 8192 Feb 28 16:35 .git
-rw-r----- 1 usr2l6ru 0 1105 Feb 28 16:35 LICENSE
-rw-r----- 1 usr2l6ru 0 836 Feb 28 16:35 README.md
-rw-r----- 1 usr2l6ru 0 19 Feb 28 16:35 contributors.txt
drwxr----- 2 usr2l6ru 0 8192 Feb 28 16:35 lib
drwxr----- 2 usr2l6ru 0 8192 Feb 28 16:35 test
We can see that the test directory exists, so let's see what it contains via the ls command.
nodejs-itoolkit/test% ls -al
total 80
drwxr-sr-x 2 usr2l6ru 0 8192 Feb 28 16:35 .
drwxr-sr-x 5 usr2l6ru 0 8192 Feb 28 16:35 ..
-rw-r--r-- 1 usr2l6ru 0 469 Feb 28 16:35 package.json
-rw-r--r-- 1 usr2l6ru 0 4324 Feb 28 16:35 test.js
Whenever I see a package.json in a shell, I use the cat command to view it. The package.json file call tell you a lot about a Node.js application.
nodejs-itoolkit/test% cat package.json
{
"name": "nodejs-itoolkit-test",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "mocha --no-exit test.js"
},
"repository": {
"type": "git",
"url": "git+https://bitbucket.org/litmis/nodejs-itoolkit.git"
},
"keywords": [
"nodejs-itoolkit"
],
"author": "Xu Meng",
"license": "MIT",
"homepage": "https://bitbucket.org/litmis/nodejs-itoolkit#readme",
"dependencies": {
"mocha": "^3.2.0"
}
}
The two things to note are the scripts and dependencies sections. In dependencies, we see the mocha npm is being used, which is a Node.js testing framework. This lets us know we need to issue an npm install to obtain the necessary npms.
nodejs-itoolkit/test% npm install
npm WARN package.json nodejs-itoolkit-test@1.0.0 No description
npm WARN package.json nodejs-itoolkit-test@1.0.0 No README data
mocha@3.2.0 node_modules/mocha
??? diff@1.4.0
??? browser-stdout@1.3.0
??? escape-string-regexp@1.0.5
??? growl@1.9.2
??? json3@3.3.2
??? commander@2.9.0 (graceful-readlink@1.0.1)
??? supports-color@3.1.2 (has-flag@1.0.0)
??? debug@2.2.0 (ms@0.7.1)
??? mkdirp@0.5.1 (minimist@0.0.8)
??? lodash.create@3.1.1 (lodash._isiterateecall@3.0.9, lodash._basecreate@3.0.3,
lodash._baseassign@3.2.0)
??? glob@7.0.5 (path-is-absolute@1.0.1, inherits@2.0.3, fs.realpath@1.0.0, once@
1.4.0, inflight@1.0.6, minimatch@3.0.3)
Now that we have mocha installed, we can attempt running the tests. Recall that there was an alias named "test" in the scripts section of packages.json. You can run those scripts by using the npm command and the script alias name.
nodejs-itoolkit/test% npm test
> nodejs-itoolkit-test@1.0.0 test nodejs-itoolkit/test
> mocha --no-exit test.js
Basic Function Test
Test iCmd()
? check the "success" property in return value (2884ms)
Test iSh()
? check the "success" property in return value (3589ms)
Test iPgm()
? check the "success" property in return value (723ms)
Test iSql()
? check the "success" property in return value (3299ms)
4 passing (12s)
All the tests passed!
Next, let's take a look at the code in test.js. We know the tests are in test.js because of the package.json file.
var assert = require('assert');
var NodeVer = process.version.slice(1,2);
assert.notEqual(NodeVer, '0', 'Unsupported version of Node.js!');
var xt = require('/QOpenSys/QIBM/ProdData/OPS/Node'+NodeVer+'/os400/xstoolkit/lib/itoolkit');
var hint = 'check the "success" property in return value'
//Need change based on your server configurations
var opt = {
db : '*LOCAL',
user : 'YOURNAME',
pwd : 'PASSWORD',
host : '8.8.8.8',
port : 8080,
path : '/cgi-bin/xmlcgi.pgm'
};
describe('Basic Function Test', function() {
this.timeout(5000)
describe('Test iCmd()', function() {
it(hint, function(done) {
var conn = new xt.iConn(opt.db);
conn.add(xt.iCmd('RTVJOBA USRLIBL(?) SYSLIBL(?)'));
conn.run(function(str){
var results = xt.xmlToJson(str);
var success = true;
results.every(function(result, i){
if(result.hasOwnProperty('success'))
success = result.success == true;
});
if(success) done();
else done(new Error(JSON.stringify(results)));
});
});
});
describe('Test iSh()', function() {
it(hint, function(done) {
var conn = new xt.iConn(opt.db);
conn.add(xt.iSh('system -i wrksyssts'));
conn.run(function(str){
var results = xt.xmlToJson(str);
var success = true;
results.every(function(result, i){
if(result.hasOwnProperty('success'))
success = result.success == true;
});
if(success) done();
else done(new Error(JSON.stringify(results)));
});
});
});
describe('Test iPgm()', function() {
it(hint, function(done) {
var conn = new xt.iConn(opt.db);
var pgm = new xt.iPgm('QWCRSVAL', {'lib':'QSYS'});
var outBuf = [
[0, '10i0'],
[0, '10i0'],
['', '36h'],
['', '10A'],
['', '1A'],
['', '1A'],
[0, '10i0'],
[0, '10i0']
];
pgm.addParam(outBuf, {'io':'out'});
pgm.addParam(66, '10i0');
pgm.addParam(1, '10i0');
pgm.addParam('QCCSID', '10A');
pgm.addParam(this.errno, {'io':'both', 'len' : 'rec2'});
conn.add(pgm);
conn.run(function(str){
var results = xt.xmlToJson(str);
var success = true;
results.every(function(result, i){
if(result.hasOwnProperty('success'))
success = result.success == true;
});
if(success) done();
else done(new Error(JSON.stringify(results)));
});
});
});
describe('Test iSql()', function() {
it(hint, function(done) {
var conn = new xt.iConn(opt.db);
var sql = new xt.iSql(); /* Test iSql Class */
sql.prepare('call qsys2.tcpip_info()');
sql.execute();
sql.fetch();
sql.free();
conn.add(sql);
conn.run(function(str){
var results = xt.xmlToJson(str);
var success = true;
results.every(function(result, i){
if(result.hasOwnProperty('success'))
success = result.success == true;
});
if(success) done();
else done(new Error(JSON.stringify(results)));
});
});
});
});
In reviewing the file, I noticed something that I believe needs fixing. Specifically, the test's success variable is defaulted to true, as shown below.
var success = true;
I believe it would be best to default it to false and require the test to prove it was successful. That's a topic for another article.
One of the things I wanted to emphasize in this article is the ability for the community to add additional tests. In reviewing the existing tests, I noticed the sql.addQuery(…) didn't yet have a test, so I figured it would be good to create one.
To create a new test, I first copied an existing one and then started making changes. Below you can see my new integration test.
it('should return SQL result set', function(done) {
var conn = new xt.iConn(opt.db);
var sql = new xt.iSql();
sql.addQuery("SELECT LSTNAM, STATE FROM QIWS.QCUSTCDT");
sql.fetch();
sql.free();
conn.add(sql);
conn.run(function(str){
var results = xt.xmlToJson(str);
var success1 = false;
var success2 = false;
results.every(function(result, i){
if(result.hasOwnProperty('success'))
success1 = result.success == true;
result.result.every(function(row, i){
success2 = row[0].hasOwnProperty('desc');
})
});
if(success1 && success2) done();
else done(new Error(JSON.stringify(results)));
});
});
I added another level of testing in this one compared to the others. I am checking not only to see whether the XMLSERVICE result was a success, but also that the value returned from the SQL statement is what I'm expecting. BTW, the iToolkit is a wrapper over the RPG-based XMLSERVICE project in case you didn't know.
To run my test, along with the other tests, I go back to the below command.
nodejs-itoolkit/test% npm test
> nodejs-itoolkit-test@1.0.0 test /home/USR2L6RU/git/nodejs-itoolkit/test
> mocha --no-exit test.js
Basic Function Test
Test iCmd()
? check the "success" property in return value (680ms)
Test iSh()
? check the "success" property in return value (2109ms)
Test iPgm()
? check the "success" property in return value (328ms)
Test iSql()
? check the "success" property in return value (1921ms)
? should return SQL result set (500ms)
5 passing (7s)
The last test shown is the new test I created. It worked!
The next step is to commit these changes to the local git repo and then push them back to the nodejs-itoolkit Bitbucket repo. First, I run git status to see what changes have been made to this repo since the beginning of this article.
nodejs-itoolkit% git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: test/test.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
test/node_modules/
In this case, I only want to commit test/test.js to the repo, so I will run the following two commands.
nodejs-itoolkit% git add test/test.js
nodejs-itoolkit% git commit -m 'Added new iSQL .addQuery(
) test'
[master 037e123] Added new iSQL .addQuery() test
1 file changed, 23 insertions(+)
And now it's time to push the changes to Bitbucket.
nodejs-itoolkit% git push origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 613 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To
ee59d1b..037e123 master -> master
Now I can visit the commit history to see the changes, which are now shared with the world. Yee-haw!
You can contribute too. In a previous MCPressOnline article, I talked about how to do this using what are called "Pull Requests." Some tests that I know need to be added include the connection side of things. Also, the current tests test the "happy path," or they focus on things we expect to work. Well, what about things we expect to break? For example, how about a test that makes sure authentication with an invalid profile and password conveys the correct error?
If you have any questions or comments, then please comment below or email me at
LATEST COMMENTS
MC Press Online