Using the Mocha xunit reporter

1,629 views
Skip to first unread message

simon drake

unread,
Jun 9, 2015, 9:22:33 AM6/9/15
to moc...@googlegroups.com
Hi all,

I have the below line in my package.json file so the output of my Mocha tests are displayed in an .xml file.

"test": "./node_modules/.bin/mocha --recursive -R xunit {testname}.js/ > test-report.xml"

This works great and gives me a summary of the running tests. My question is; is there anyway of inputting custom text into this file? I have a test which basically goes to my website and through Asynchronous operations moves onto the next promise or throws an error.

What I would like to do is, where successful, also have something logged (e.g. "Logo Element found") into the XML file but the 'console.log' operation just screws up the .xml file and sticks the text at the top of the file instead of under the test.

Not sure if this is possible but would appreciate any help....

Thanks!

Vlad GURDIGA

unread,
Jun 10, 2015, 4:10:15 AM6/10/15
to moc...@googlegroups.com
Hey Simon,

It looks like the xunit-reporter writes to the XML report the full nesting path — the concatenated text from nested describe()s — into the classname attribute of <testcase>, and the particular test description — the text in the it() — into the name attribute.

Here is a quick test:

    ~/tmp ɀ  cat test.js 

describe('Website UI', function() {
describe('Logo', function() {
it('was found', function() {
});
});
});

    ~/tmp ɀ  mocha -R xunit test.js 

<testsuite name="Mocha Tests" tests="1" failures="0" errors="0" skipped="0" timestamp="Wed, 10 Jun 2015 07:54:35 GMT" time="0.003">
<testcase classname="Website UI Logo" name="was found" time="0.001"/>
</testsuite>

    ~/tmp ɀ  


I have a feeling that I maybe have misunderstood the question, :-> so if that’s the case and you wanted “Logo Element found” into the console, then you can try using console.erorr() so that it doesn’t go to the .xml file when using output redirection with “> test-report.xml”.

Let me know if I’ve missed the target. :o)

simon drake

unread,
Jun 10, 2015, 5:59:35 AM6/10/15
to moc...@googlegroups.com
Hi Vlad :-)

Thank you for responding! I'm not quite sure that this gives me what I'm looking for.... Using your example I've adapted it slightly to what my test currently looks like:

describe('Website UI', function() {
it('check if elements exist', function() {
  Class.{methodname}()
});
});

The class/method then uses a promise (like the below):

    this.findLogo = function(){
        return this.driver.findElement(this.webdriver.By.css(elementLogo))
            .then(function (logoElement){
                    console.log('logo element has been found');
        }, function (logoError){
            throw new Error ('unable to find Logo Element');
        });
    };

Where I would then like the console.log statement to be included in the xml file (using your example) like this:

<testsuite name="Mocha Tests" tests="1" failures="0" errors="0" skipped="0" timestamp="Wed, 10 Jun 2015 07:54:35 GMT" time="0.003">
<testcase classname="Website UI Logo" name="was found" time="0.001"/>
logo element has been found
version element has been found
username element has been found
</testsuite>

But instead the console.log prints out at the top of the xml file messing everything up.



I hope that makes more sense :-) thanks in advance!

Vlad GURDIGA

unread,
Jun 10, 2015, 6:35:09 AM6/10/15
to moc...@googlegroups.com
OK, now I think I understand. :)

The way I’d go about it is to have an it() block for every promise-based method that verifies something. Mocha is promise aware, which means that you simply return the promise from the it() block and the test will pass if the promise is resolved, or fails if the promise is rejected.

describe('Website UI', function() {
it('displays logo', function() {
return UIVerifier.verifyLogo();
});
 
it('displays copyright', function() {
return UIVerifier.verifyCopyright();
});
});
 
var UIVerifier = {
verifyLogo: function() {
return Q.Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error('Logo not found'));
}, 100)
});
},
 
verifyCopyright: function() {
return Q.Promise(function(resolve, reject) {
setTimeout(function() {
resolve();
}, 100)
});
}
};
 
var Q = require('q');

Running this will give me this xunit report:

<testsuite name="Mocha Tests" tests="2" failures="1" errors="1" skipped="0" timestamp="Wed, 10 Jun 2015 10:30:03 GMT" time="0.223">
<testcase classname="Website UI" name="displays logo" time="0.11"><failure><![CDATA[Logo not found
Error: Logo not found
at null._onTimeout (/Users/vlad/tmp/test.js:15:16)
at Timer.listOnTimeout (timers.js:110:15)]]></failure></testcase>
<testcase classname="Website UI" name="displays copyright" time="0.106"/>
</testsuite>

This doesn’t report the successes exactly¹ as you wanted them — as CDATA inside below the <testcase/> element — but it gives the relevant information about the tests. If you need the test report differently, maybe you need a different reporter? :-)

¹ this is probable more a xunit report format constraint than Mocha constraint

simon drake

unread,
Jun 10, 2015, 6:40:14 AM6/10/15
to moc...@googlegroups.com
Ah I see what you mean! Thank you!


I need to have another describe function rather than have everything under one! I'll give this a go :-) thank you!

--
You received this message because you are subscribed to a topic in the Google Groups "Mocha" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mochajs/foz3ilI1qvo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mochajs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

simon drake

unread,
Jun 10, 2015, 8:45:56 AM6/10/15
to moc...@googlegroups.com
Hi Vlad,

So I changed my code (below)

test.describe('Check all elements exist',function(){
    test.it('Check username exists'), function(){
        return login.findUsername();
    };
    test.it('Check password exists'), function(){
        return login.findPassword();
    };
    test.it('Check Logo exists'), function(){
        return login.findLogo()
    };
    test.it('Check Go exists'), function(){
        return login.findGoButton()
    };
    test.it('Check Version Footer exists'), function(){
        return login.findVersionFooter()
    };
    test.it('Check Version is correct'), function(){
        return login.confirmVersion(versionFooterVersion);
    };
   
});


But now the tests are being 'skipped' as shown in the below xml report. I've no idea why it's doing that :-S!

<?xml version="1.0"?>
-<testsuite time="129.695" timestamp="Wed, 10 Jun 2015 12:38:57 GMT" skipped="6" errors="0" failures="0" tests="8" name="Mocha Tests">
<testcase time="0.599" name="Check that username and password can be entered" classname="Login Page Tests"/>
<testcase time="100.861" name="Check all users can login" classname="Login Page Tests"/>
-<testcase time="0" name="Check username exists" classname="Login Page Tests Check all elements exist">
<skipped/>
</testcase>-<testcase time="0" name="Check password exists" classname="Login Page Tests Check all elements exist">
<skipped/>
</testcase>-
<testcase time="0" name="Check Logo exists" classname="Login Page Tests Check all elements exist">
<skipped/>
</testcase>-
<testcase time="0" name="Check Go exists" classname="Login Page Tests Check all elements exist">
<skipped/>
</testcase>
-<testcase time="0" name="Check Version Footer exists" classname="Login Page Tests Check all elements exist">
<skipped/>
</testcase>-
<testcase time="0" name="Check Version is correct" classname="Login Page Tests Check all elements exist">
<skipped/>
</testcase>
</testsuite>

Vlad GURDIGA

unread,
Jun 10, 2015, 10:01:38 AM6/10/15
to mochajs
Hey Simon,

It looks like the parentheses for the it() blocks are closes improperly… :o)


-----------------------------v
    test.it('Check Go exists'), function(){
        return login.findGoButton()
    };

     ^-------- the parentheses should close here

Without a body, a test in considered “skipped.” :-)

simon drake

unread,
Jun 10, 2015, 10:28:03 AM6/10/15
to moc...@googlegroups.com
Hi Vlad,

Of course it does! What a rookie mistake! Still trying to get to grips with Javascript/Mocha as I came from Java/JUnit!

Thank you for all the help; you've been most helpful!

Si

--

Vlad GURDIGA

unread,
Jun 10, 2015, 10:32:36 AM6/10/15
to mochajs
Glad to have been helpful, good luck! ;-)

P.S. take a look at JSHint when you get a chance, it’s helpful in catching errors like that last one. ;-)

simon drake

unread,
Jun 10, 2015, 10:38:28 AM6/10/15
to moc...@googlegroups.com
Thanks mate :-)

I've got some difficult stuff coming up in the next few days so I'll bare that in mind! thank you :-)

On 10 June 2015 at 15:32, Vlad GURDIGA <gur...@gmail.com> wrote:
Glad to have been helpful, good luck! ;-)

P.S. take a look at JSHint when you get a chance, it’s helpful in catching errors like that last one. ;-)

--
Reply all
Reply to author
Forward
0 new messages