sj26 / rspec_junit_formatter

RSpec results that your CI can read
http://rubygems.org/gems/rspec_junit_formatter
MIT License
302 stars 122 forks source link

[FEATURE] Show line number in file path #62

Closed maschwenk closed 6 years ago

maschwenk commented 6 years ago

Shows the line number in the file path

<testcase 
  classname="spec.commands.speccy_the_spectacle"
  name="Thing::Guy#call should be successful"
  file="./spec/commands/speccy_the_spectacle.rb:5"
  time="0.381122">
</testcase>

This is really helpful because it means I can literally take the failed example and paste directly into my CLI to run the test, rather than hunting down for the actual example that failed within the file

sj26 commented 6 years ago

Unfortunately, the junit xml standard does not include a line number in the file attribute, and this will break current consumers of the xml output. We also considered adding a new attribute in #38, but it broke other consumers of the xml output which rely on xsd validation.

The best we can offer here is relying on the stack trace. Rspec should be adding a stack trace to the failure messsage, including the file and line number for re-running:

<testcase classname="spec.example_spec" name="some example specs shows diffs cleanly" file="./spec/example_spec.rb" time="0.001777"><failure message="
expected: {:a=&gt;2, :c=&gt;4}
     got: {:a=&gt;&quot;b&quot;, :c=&gt;&quot;d&quot;}

(compared using eql?)

Diff:
@@ -1,3 +1,3 @@
-:a =&gt; 2,
-:c =&gt; 4,
+:a =&gt; &quot;b&quot;,
+:c =&gt; &quot;d&quot;,
" type="RSpec::Expectations::ExpectationNotMetError">Failure/Error: expect({a: &quot;b&quot;, c: &quot;d&quot;}).to eql({a: 2, c: 4})

  expected: {:a=&gt;2, :c=&gt;4}
       got: {:a=&gt;&quot;b&quot;, :c=&gt;&quot;d&quot;}

  (compared using eql?)

  Diff:
  @@ -1,3 +1,3 @@
  -:a =&gt; 2,
  -:c =&gt; 4,
  +:a =&gt; &quot;b&quot;,
  +:c =&gt; &quot;d&quot;,

./spec/example_spec.rb:26:in `block (2 levels) in &lt;top (required)&gt;&apos;
./spec/spec_helper.rb:7:in `block (2 levels) in &lt;top (required)&gt;&apos;</failure></testcase>

Do you have specs which aren't producing a stack trace? Can you provide an example?

sj26 commented 6 years ago

On review, I'm not even sure that the file attribute is included in the xsd either. Maybe we should just add another attribute? Idk.

sj26 commented 6 years ago

The canonical junit implementation is here:

https://github.com/junit-team/junit5/blob/master/junit-platform-console/src/main/java/org/junit/platform/console/tasks/XmlReportWriter.java

sj26 commented 6 years ago

Note that if you just want machine-readable output, you can use rspec's built-in json output:

https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/formatters/json_formatter.rb

If you specifically want something to be consumed by a junit parser, we need something that's valid output, hence my caution.

maschwenk commented 6 years ago

@sj26 Understood, I just wanted to avoid regex parsing a stacktrace but maybe that's not so bad if the format is standard