Search code examples
node.jsjenkinsmocha.jsxunitweb-api-testing

How to append extra information based on mocha + jenkins framework?


Background

I am using mocha.js to perform api automation while using jenkins to implement continuous integration. I am facing some issues while trying to logging some extra information for failed tests.

My code

Following are my basic code for single api testing.

var conf = require('../../../configuration.js');
var CONST = conf.CONST;
var R = require('../../../req.js');
var expect = R.expect;
var __path = R.__path;
var Promise = require('bluebird');
var supertest = R.supertest;


var env = CONST.APP_ADDRESS_TESTENV;
var tester = supertest.agent(env);


describe('TestA', function () {

    it('TestPoint A', function (done) {
        var url = __path(__filename);
        var params = 'languageId=1';
        tester.get(url + params)
            .end(function (err, res) {
                new Promise(function (resolve, reject) {
                    var result = res.body.result;
                    expect(result.length).equal(8);
                    resolve(res.body);
                }).then(body => {
                    expect(body.msg).equal("True");
                    return body;
                }).then(body => {
                    expect(body.code).equal("0");
                    done();
                    return body;
                }).catch(err => {
                    console.log(env + url + params);
                    console.log(JSON.stringify(res.body));
                    done(err);
                });
            });
    });
});

Question When I run local test, for example, directly run mocha *.js, then the script goes well. If something wrong, it would fail the tests and print mocha exception. Also it would output the information I need(by console.log)

When it comes to jenkins, yea i can also do this in the same way and it can work fine. But for jenkins, i need to use 'Xunit reporter' of mocha, which would generate a reporter xml and read by jenkins. Then jenkins is easy to collect real time and historical test information and do further statistics. But when above code goes to jenkins, however, it will break the xml and throw Exception like

org.dom4j.DocumentException: Error on line 1 of document file:/

I know this is due to "console.log" but i have no idea about this. I just want to see those information if some cases are failed, no matter where it's located(jenkins console or xml report).


Solution

  • Oh I have found I can pass all I want as parameters of done() . This may not a big deal. Thanks for all you guys attention