Repository: mochajs/mocha

Problem statement:
xunit reporter does not report <testcase />
currently Xunit reporter only reports the top

```
<testsuite name="Mocha Tests" tests="16" failures="0" errors="0" skipped="0" timestamp="Fri, 04 Sep 2015 23:05:49 GMT" time="0.043">`
```

but not the child test cases
something that i would expect is:

```
<testsuite name="Mocha Tests" tests="16" failures="0" errors="0" skipped="0" timestamp="Fri, 04 Sep 2015 23:35:14 GMT" time="0.043">
  <testcase classname="case1" name="abc" time="0.2"/>
  <testcase classname="case2" name="cde" time="0.1"/>
</testsuite>
```

is there a reason for it?



Hints:
quite possible it's broken since we don't have good tests for the reporters

yep. since i upgraded mocha from 2.2.4 to 2.3.0.

using:

```
--reporter xunit --reporter-options output=build/testreport-integration.xml
```

only writes the first line:

```
<testsuite name="Mocha Tests" tests="242" failures="0" errors="0" skipped="21" timestamp="Fri, 04 Sep 2015 18:55:09 GMT" time="25.353">
```

Just to help confirm this.

Stupid simple test file

```
> cat test.js
describe('my test', function() {
    it('should pass', function(done) {
        done();
    });

    it('should fail', function(done) {
        done(new Error('fail'));
    });
});
```

Works as expected in `2.2.5`

```
> mocha --version
2.2.5

> mocha --reporter=xunit test.js
<testsuite name="Mocha Tests" tests="2" failures="1" errors="1" skipped="0" timestamp="Wed, 09 Sep 2015 18:29:50 GMT" time="0.006">
<testcase classname="my test" name="should pass" time="0.001"/>
<testcase classname="my test" name="should fail" time="0"><failure><![CDATA[fail
Error: fail
    at Context.&lt;anonymous&gt; (test.js:7:8)]]></failure></testcase>
</testsuite>

> mocha --reporter=xunit --reporter-options output=results.xml test.js; cat results.xml
<testsuite name="Mocha Tests" tests="2" failures="1" errors="1" skipped="0" timestamp="Wed, 09 Sep 2015 18:29:56 GMT" time="0.007">
<testcase classname="my test" name="should pass" time="0.001"/>
<testcase classname="my test" name="should fail" time="0"><failure><![CDATA[fail
Error: fail
    at Context.&lt;anonymous&gt; (test.js:7:8)]]></failure></testcase>
</testsuite>
```

But not `2.3.0`

```
> mocha --version
2.3.0

> mocha --reporter=xunit test.js
<testsuite name="Mocha Tests" tests="2" failures="1" errors="1" skipped="0" timestamp="Wed, 09 Sep 2015 18:31:10 GMT" time="0.007">
<testcase classname="my test" name="should pass" time="0.001"/>
<testcase classname="my test" name="should fail" time="0"><failure><![CDATA[fail
Error: fail
    at Context.&lt;anonymous&gt; (test.js:7:8)]]></failure></testcase>
</testsuite>

> mocha --reporter=xunit --reporter-options output=results.xml test.js; cat results.xml                                                           
<testsuite name="Mocha Tests" tests="2" failures="1" errors="1" skipped="0" timestamp="Wed, 09 Sep 2015 18:31:12 GMT" time="0.007">
```


Patch:
diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js
--- a/lib/reporters/xunit.js
+++ b/lib/reporters/xunit.js
@@ -77,6 +77,11 @@ function XUnit(runner, options) {
   });
 }
 
+/**
+ * Inherit from `Base.prototype`.
+ */
+inherits(XUnit, Base);
+
 /**
  * Override done to close the stream (if it's a file).
  *
@@ -93,11 +98,6 @@ XUnit.prototype.done = function(failures, fn) {
   }
 };
 
-/**
- * Inherit from `Base.prototype`.
- */
-inherits(XUnit, Base);
-
 /**
  * Write out the given line.
  *


Test patch:
diff --git a/test/integration/reporters.js b/test/integration/reporters.js
--- a/test/integration/reporters.js
+++ b/test/integration/reporters.js
@@ -1,8 +1,12 @@
 var assert = require('assert');
+var os     = require('os');
+var fs     = require('fs');
+var crypto = require('crypto');
+var path   = require('path');
 var run    = require('./helpers').runMocha;
 
 describe('reporters', function() {
-  this.timeout(1000);
+  this.timeout(3000);
 
   describe('markdown', function() {
     var res;
@@ -28,4 +32,29 @@ describe('reporters', function() {
       assert(res.output.indexOf(src) !== -1, 'No assert found');
     });
   });
+
+  describe('xunit', function() {
+    it('prints test cases with --reporter-options output (issue: 1864)', function(done) {
+      var randomStr = crypto.randomBytes(8).toString('hex');
+      var tmpDir = os.tmpDir().replace(new RegExp(path.sep + '$'), '');
+      var tmpFile = tmpDir + path.sep + 'test-issue-1864-' + randomStr + '.xml';
+
+      var args = ['--reporter=xunit', '--reporter-options', 'output=' + tmpFile];
+      var expectedOutput = [
+        '<testcase classname="suite" name="test1" time="0"/>',
+        '<testcase classname="suite" name="test2" time="0"/>',
+        '</testsuite>'
+      ].join('\n');
+
+      run('passing.js', args, function(err, result) {
+        if (err) return done(err);
+
+        var xml = fs.readFileSync(tmpFile, 'utf8');
+        fs.unlinkSync(tmpFile);
+
+        assert(xml.indexOf(expectedOutput) !== -1, 'Did not output all xml');
+        done(err);
+      });
+    });
+  });
 });