1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
# RSpec JUnit Formatter
[](https://travis-ci.org/sj26/rspec_junit_formatter)
[](https://rubygems.org/gems/rspec_junit_formatter)
[RSpec][rspec] 2 & 3 results that your CI can read. [Jenkins][jenkins-junit], [Buildkite][buildkite-junit], [CircleCI][circleci-junit], and probably more, too.
[rspec]: http://rspec.info/
[jenkins-junit]: https://jenkins.io/doc/pipeline/steps/junit/
[buildkite-junit]: https://github.com/buildkite/rspec-junit-example
[circleci-junit]: https://circleci.com/docs/2.0/collect-test-data/
## Usage
Install the gem:
```sh
gem install rspec_junit_formatter
```
Use it:
```sh
rspec --format RspecJunitFormatter --out rspec.xml
```
You'll get an XML file `rspec.xml` with your results in it.
You can use it in combination with other [formatters][rspec-formatters], too:
```sh
rspec --format progress --format RspecJunitFormatter --out rspec.xml
```
[rspec-formatters]: https://relishapp.com/rspec/rspec-core/v/3-6/docs/formatters
### Using in your project with Bundler
Add it to your Gemfile if you're using [Bundler][bundler]. Put it in the same groups as rspec.
```ruby
group :test do
gem "rspec"
gem "rspec_junit_formatter"
end
```
Put the same arguments as the commands above in [your `.rspec`][rspec-file]:
```sh
--format RspecJunitFormatter
--out rspec.xml
```
[bundler]: https://bundler.io
[rspec-file]: https://relishapp.com/rspec/rspec-core/v/3-6/docs/configuration/read-command-line-configuration-options-from-files
### Parallel tests
For use with `parallel_tests`, add `$TEST_ENV_NUMBER` in the output file option (in `.rspec` or `.rspec_parallel`) to avoid concurrent process write conflicts.
```sh
--format RspecJunitFormatter
--out tmp/rspec<%= ENV["TEST_ENV_NUMBER"] %>.xml
```
The formatter includes `$TEST_ENV_NUMBER` in the test suite name within the XML, too.
### Capturing output
If you like, you can capture the standard output and error streams of each test into the `:stdout` and `:stderr` example metadata which will be added to the junit report, e.g.:
```ruby
# spec_helper.rb
RSpec.configure do |config|
# register around filter that captures stdout and stderr
config.around(:each) do |example|
$stdout = StringIO.new
$stderr = StringIO.new
example.run
example.metadata[:stdout] = $stdout.string
example.metadata[:stderr] = $stderr.string
$stdout = STDOUT
$stderr = STDERR
end
end
```
## Caveats
* XML can only represent a [limited subset of characters][xml-charsets] which excludes null bytes and most control characters. This gem will use character entities where possible and fall back to replacing invalid characters with Ruby-like escape codes otherwise. For example, the null byte becomes `\0`.
[xml-charsets]: https://www.w3.org/TR/xml/#charsets
## Roadmap
* It would be nice to split things up into individual test suites, although would this correspond to example groups? The subject? The spec file? Not sure yet.
## Development
Run the specs with `bundle exec rake`, which uses [Appraisal][appraisal] to run the specs against all supported versions of rspec.
[appraisal]: https://github.com/thoughtbot/appraisal
## Releasing
Bump the gem version in the gemspec, and commit. Then `bundle exec rake build` to build a gem package, `bundle exec rake install` to install and test it locally, then `bundle exec rake release` to tag and push the commits and gem.
## License
The MIT License, see [LICENSE](./LICENSE).
## Thanks
Inspired by the work of [Diego Souza][dgvncsz0f] on [RSpec Formatters][dgvncsz0f/rspec_formatters] after frustration with [CI Reporter][ci_reporter].
[dgvncsz0f]: https://github.com/dgvncsz0f
[dgvncsz0f/rspec_formatters]: https://github.com/dgvncsz0f/rspec_formatters
[ci_reporter]: https://github.com/nicksieger/ci_reporter
|