8ball-media

little pieces from everything

CasperJS - fail test with custom testcase-name in xml report

It took some time, but my PR to CasperJS has landed. Now you can fail a test with a custom value for a testcase node’s name property like:

1
casper.test.fail('A custom message', {name: 'custom name'});

tl;dr

When using the --xunit=/path/to/report.xml casperjs is using the message as property for the name of the testcase node in xml report.

This means if you fail a test like:

1
casper.fail('A really long and expressive message');

the resulting xml would look something like:

1
2
3
<testcase classname="a-custom-test-case-name" name="A really long and expressive message" time="0.023">
  <failure type="fail">A really long and expressive message</failure>
</testcase>

Imagine you want to capture and provide an stacktrace from a jsp page as message..

Here comes the optional argument FTW. As stated above, pass in a arbitrary object with a name property like:

1
2
var stacktrace = casper.fictional_method_to_capture_stacktrace();
casper.test.fail(stacktrace, {name: 'stacktrace'});

and you’ll get

1
2
3
4
5
6
<testcase classname="a-custom-test-case-name" name="stacktrace" time="0.023">
  <failure type="fail">Exception in thread "main" java.lang.NullPointerException
    at Test.test(Test.java:6)
    at Test.main(Test.java:10)
  </failure>
</testcase>

Much better..