Skip to content

UnitFormatter for cucumber-jvm #226

Merged
merged 3 commits into from Feb 29, 2012

3 participants

@trnl
trnl commented Feb 28, 2012

This unit formatter can be used while running cucumber via cli.

Also, with this it is become possible to generate html and unit reports and the same time. Previously we have to decide - html via cli or unit via cucumber-junit.

@remast
remast commented Feb 28, 2012

Hi,

how can you use that to create both standard unit test reports and html reports? That's exactly what I am looking for!

Thanx

@trnl
trnl commented Feb 28, 2012

Yeah,

Just run in the following way:

-f pretty -f unit -o reports/unit.xml -f html -o reports/

I'm doing this for jenkins.

-vova

@remast
remast commented Feb 28, 2012

I am a bit confused that you don't use the cucumber HtmlFormatter. So your Html report contains the unit tests but not the feature information am I right?

@trnl
trnl commented Feb 28, 2012

nope, to create multiply reports you just need to type all required formatters. Please, note -f html later in the line.

-vova

@aslakhellesoy
Cucumber member

Looks great. I'll probably merge this in next week.

@aslakhellesoy aslakhellesoy commented on the diff Feb 29, 2012
core/src/main/java/cucumber/formatter/HTMLFormatter.java
-import gherkin.formatter.model.Feature;
-import gherkin.formatter.model.Match;
-import gherkin.formatter.model.Result;
-import gherkin.formatter.model.Scenario;
-import gherkin.formatter.model.ScenarioOutline;
-import gherkin.formatter.model.Step;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
+import gherkin.formatter.model.*;
@aslakhellesoy
Cucumber member

It would be great if you could configure your IDE to not do star imports - see the README for coding standards

@trnl
trnl added a note Feb 29, 2012

Sure, I've noticed it after submitting pull request. Do I need another commit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@aslakhellesoy aslakhellesoy commented on the diff Feb 29, 2012
core/src/main/java/cucumber/formatter/UnitFormatter.java
+ private TestCase() {
+ }
+
+ Scenario scenario;
+ static Feature feature;
+ static int examples = 0;
+ List<Step> steps = new ArrayList<Step>();
+ List<Result> results = new ArrayList<Result>();
+
+ private Element writeTo(Document doc) {
+ Element tc = doc.createElement("testcase");
+ tc.setAttribute("classname", feature.getName());
+ tc.setAttribute("name", examples > 0 ? scenario.getName() + "_" + examples-- : scenario.getName());
+ long time = 0;
+ for (Result r : results) {
+ time += r.getDuration() != null ? r.getDuration() : 0;
@aslakhellesoy
Cucumber member

Result.getDuration() is currently in nanoseconds. I'm not sure what the JUnit report contract is, but I assume millis.

What do you think - should I rename this to getDurationNanos() ? That would make it more obvious that people who want millis (you?) have to divide by 1000000.

@trnl
trnl added a note Feb 29, 2012

I think this this would be better if you need nanos somewhere else.

public long getDuration() {
    this. getDurationNanos()/1000000
}

I just noticed that my tests running for years :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@aslakhellesoy aslakhellesoy commented on the diff Feb 29, 2012
...c/test/java/cucumber/formatter/UnitFormatterTest.java
+ }
+
+ @Test
+ public void featureWithBackgroundTest() throws Exception {
+ runFeaturesWithFormatter(asList("cucumber/formatter/UnitFormatterTest_2.feature"));
+ compareXML("cucumber/formatter/UnitFormatterTest_2.report.xml", "report.xml");
+ }
+
+ @Test
+ public void featureWithOutlineTest() throws Exception {
+ runFeaturesWithFormatter(asList("cucumber/formatter/UnitFormatterTest_3.feature"));
+ compareXML("cucumber/formatter/UnitFormatterTest_3.report.xml", "report.xml");
+ }
+
+ private void runFeaturesWithFormatter(final List<String> featurePaths) throws IOException {
+ File report = new File("report.xml");
@aslakhellesoy
Cucumber member

I think File.createTempFile would be better here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@aslakhellesoy aslakhellesoy commented on the diff Feb 29, 2012
core/src/test/resources/cucumber/formatter/test.xsd
@@ -0,0 +1,91 @@
@aslakhellesoy
Cucumber member

Can you add a comment pointing to where this file comes from? I'm assuming Ant's source code, but not sure...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@trnl trnl commented on the diff Feb 29, 2012
core/src/test/resources/cucumber/formatter/test.xsd
+ <xs:element name="testsuites">
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element ref="testsuite" minOccurs="0" maxOccurs="unbounded"/>
+ </xs:sequence>
+ <xs:attribute name="name" type="xs:string" use="optional"/>
+ <xs:attribute name="time" type="xs:string" use="optional"/>
+ <xs:attribute name="tests" type="xs:string" use="optional"/>
+ <xs:attribute name="failures" type="xs:string" use="optional"/>
+ <xs:attribute name="disabled" type="xs:string" use="optional"/>
+ <xs:attribute name="errors" type="xs:string" use="optional"/>
+ </xs:complexType>
+ </xs:element>
+
+
+</xs:schema>
@trnl
trnl added a note Feb 29, 2012

This file is not used. Should be removed.

But we can use it while testing. Haven't done this yet. Do you think it will be helpful?

@aslakhellesoy
Cucumber member

I definitely think it would be helpful for testing - as long as this is the official schema. Is it? Did you write it or does it come from JUnit or Ant?

@aslakhellesoy
Cucumber member

The latter one looks more thorough, but it's LGPL and we can't have LGPL code in cucumber-jvm.

I'm asking the author (whom I happen to know) to waive the LGPL for us. Will update here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@aslakhellesoy aslakhellesoy merged commit 6bf58e6 into cucumber:master Feb 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.