Enabling Behaviour driven documentation!
BDD living documentation using Cucumber and Asciidoctor based on Cucumber JSON execution output.
In order to have awesome living documentation
As a bdd developer
I want to convert my test results into Asciidoc format.
GIVEN I execute my cucumber tests using the json formatter
AND cucumber json output files are generated
WHEN I convert the files using Cukedoctor
THEN I should have awesome living documentation based on asciidoc.
As a proof of concept, Cukedoctor bdd tests output are rendered by itself:
ℹ️
|
This documentation is published to gh-pages by travisci on each successful build. |
Here are some bdd documentation examples generated by Cukedoctor:
Project | Living documentation |
---|---|
Cukedoctor converter is the basis for other modules, it generates asciidoc files based on Cucumber json execution files.
Just declare the plugin in your pom.xml:
<dependency>
<groupId>com.github.cukedoctor</groupId>
<artifactId>cukedoctor-converter</artifactId>
<version>1.0.6</version>
</dependency>
@Test
public void shouldSaveDocumentationIntoDisk(){
List<String> pathToCucumberJsonFiles = FileUtil.findJsonFiles("target/test-classes/json-output/");
List<Feature> features = FeatureParser.parse(pathToCucumberJsonFiles);
DocumentAttributes attrs = GlobalConfig.getInstance().getDocumentAttributes;
attrs.toc("left").backend("html5")
.docType("book")
.icons("font").numbered(false)
.sourceHighlighter("coderay")
.docTitle("Documentation Title")
.sectAnchors(true).sectLink(true);
CukedoctorConverter converter = Cukedoctor.instance(features, attrs);
converter.setFilename("target/living_documentation.adoc");
converter.saveDocumentation();
assertThat(FileUtil.loadFile("target/living_documentation.adoc")).exists();
}
❗
|
To generate cucumber .json output files just execute your BDD tests with json formatter, example: @RunWith(Cucumber.class)
@CucumberOptions(plugin = {"json:target/cucumber.json"} )
|
You can add a custom introduction chapter to your living documentations by placing a file named cukedoctor-intro.adoc anywhere on your classpath.
The content of the file will be placed between Documentation title and summary section. Here’s an example of cukedoctor-intro.adoc:
= *This is a sample introduction chapter*
Introduction chapter is the place where you can insert custom content for your living documentation.
=== Sub section
Introduction chapter can have subsections
Here is rendered documentation:
Cukedoctor can use internationalization in two flavours:
Cucumber feature languages are provided via comments in a feature file, see here for examples.
If your feature language is not supported by Cukedoctor you can contribute it here or use a custom bundle.
Another way of internationalization is to provide a custom bundle.
If you do so Cukedoctor will ignore feature language and will use provided resource bundle.
The name of the file must be cukedoctor.properties and can be anywhere in your classpath.
Here are the key values you must provide to customize your documentation:
#sections
title.features = Features
title.summary = Summary
title.scenario = Scenario
#summary
summary.steps = Steps
summary.total = Totals
summary.duration = Duration
#result
result.passed = Passed
result.failed = Failed
result.skipped = Skipped
result.pending = Pending
result.undefined= Undefined
result.missing = Missing
Cukdoctor currently supports the following locales en, es, fr, ge and pt.
Here are the supported locales
Cukedoctor comes with some extensions to enhance and customize its documentation content, for more details see Cukedoctor extensions module.
To enrich the documentation one can use asciidoc markup inside Cucumber feature files, consider the following feature:
Feature: Calculator
Scenario: Adding numbers
You can asciidoc markup in feature description.
Given I have numbers 1 and 2
When I sum the numbers
Then I should have 3 as result
It will be rendered by Cukedoctor as follows:
Now if you want to enrich your living documentation you can use asciidoc syntax in your feature:
Feature: Calculator
Scenario: Adding numbers
You can use *asciidoc markup* in _feature_ #description#.
NOTE: This is a very important feature!
#{IMPORTANT: Asciidoc markup inside *steps* must be surrounded by *curly brackets*.}
Given I have numbers 1 and 2
# {NOTE: Steps comments are placed *before* each steps so this comment is for the *WHEN* step.}
When I sum the numbers
# {* this is a list of itens inside a feature step}
# {* there is no multiline comment in gherkin}
# {** second level list item}
Then I should have 3 as result
And it will be rendered as follows:
You can use Asciidoc markup in feature DocStrings, see feature below:
Feature: Discrete class feature
Scenario: Render source code
# cukedoctor-discrete
Given the following source code
"""
[source, java]
-----
public int sum(int x, int y){
int result = x + y;
return result; (1)
}
-----
<1> We can have callouts in living documentation
"""
Scenario: Render table
# cukedoctor-discrete
Given the following table
"""
|====
| Cell in column 1, row 1 | Cell in column 2, row 1
| Cell in column 1, row 2 | Cell in column 2, row 2
| Cell in column 1, row 3 | Cell in column 2, row 3
|====
"""
The docstrings will be rendered as follows:
❗
|
By default Cukedoctor will render DocStrings as asciidoc listing. To enable this feature use # cukedoctor-discrete comment. |
Imagine you don’t want to automate a feature (because e.g you don’t have time) or you simple don’t like the Given When Then BDD way of describing features.
You still can write the hole feature documentation (using asciidoc
) in feature and scenario description
without writing any cucumber step.
Here is an example feature and resulting documentation.
ℹ️
|
Not automated doesn’t mean you didn’t discussed the feature and it’s scenarios. |
Cukedoctor let you customize its generated documentation through Java service provider mechanism, see Cukedoctor-spi-example for example code.
Cukedoctor documentation customization is done through Java service provider mechanism.
You just need to implement one of the interfaces in Cukedoctor SPI and declare it in META-INF/services.
Given this cucumber feature:
Feature: Calculator
Scenario: Adding numbers
Given I have numbers 1 and 2
When I sum the numbers
Then I should have 3 as result
Scenario: Subtracting numbers
A feature with a failing step
Given I have numbers 2 and 1
When I subtract the numbers
Then I should have 0 as result
When we generate documentation using default cukedoctor renderers we got the following result:
To customize summary one have to implement SummaryRenderer interface. Here is an example:
public class CustomSummaryRenderer extends AbstractBaseRenderer implements SummaryRenderer {
@Override
public String renderSummary(List<Feature> features) {
docBuilder.textLine(H2(bold(i18n.getMessage("title.summary"))));
docBuilder.textLine("This is a custom summary renderer").newLine();
docBuilder.textLine("Number of features: "+features.size());
docBuilder.newLine();
ScenarioTotalizations totalization = new ScenarioTotalizations(features);
docBuilder.append("Passed steps: ",totalization.getTotalPassedSteps(),newLine())
.append(newLine()).append("Failed steps: ", totalization.getTotalFailedSteps(),newLine());
return docBuilder.toString();
}
}
ℹ️
|
Abstract renderer is a template class which provides implementation of helper methods. |
Now Imagine we want to render features as Asciidoctor labeled lists instead of sections, see prototype below:
To do that you need to implement FeatureRenderer and also ScenarioRenderer.
public class CustomFeatureRenderer extends CukedoctorFeatureRenderer {(1)
@Override
public String renderFeature(Feature feature) {
docBuilder.textLine((bold(feature.getName()))+"::").newLine();
if (hasText(feature.getDescription())) {
docBuilder.append("+").sideBarBlock(feature.getDescription().trim().replaceAll("\\n", " +" + newLine()));
}
if(feature.hasScenarios()){
ScenarioRenderer scenarioRenderer = new CustomScenarioRenderer();
for (Scenario scenario : feature.getScenarios()) {
docBuilder.append(scenarioRenderer.renderScenario(scenario,feature));(2)
}
}
return docBuilder.toString();
}
}
-
You can also extend default renderers as above.
-
Here we provide a custom ScenarioRenderer but you could embed all markup in FeatureRenderer if you want but depending on complexity things can get messy.
And finally here is the custom ScenarioRenderer:
public class CustomScenarioRenderer extends CukedoctorScenarioRenderer{
@Override
public String renderScenario(Scenario scenario, Feature feature) {
//need to clear because we will execute this method in a for loop
//and contents will be appended
docBuilder.clear();
docBuilder.append(" "+scenario.getName()+":::",newLine());
if(scenario.hasSteps()) {
//here we will reuse builtin step renderer
docBuilder.textLine("+");
StepsRenderer stepsRenderer = new CukedoctorStepsRenderer();(1)
docBuilder.append(stepsRenderer.renderSteps(scenario.getSteps()));
}
return docBuilder.toString();
}
}
-
Here we leverage default StepsRenderer that comes with Cukedoctor.
Now the output of our customized living documentation:
❗
|
Don’t forget to register your custom implementations in META-INF/services directory. |
This module brings the ability to execute Cukedoctor converter through a maven plugin.
The plugin just scans .json cucumber execution files in target dir and generates asciidoc documentation on target/cukedoctor folder.
Just declare the plugin in your pom.xml:
<plugin>
<groupId>com.github.cukedoctor</groupId>
<artifactId>cukedoctor-maven-plugin</artifactId>
<version>1.0.6</version>
<executions>
<execution>
<goals>
<goal>execute</goal>
</goals>
<phase>install</phase> (1)
</execution>
</executions>
</plugin>
-
You need to use a phase that runs after your tests, see maven lifecycle.
❗
|
To generate cucumber .json output files just execute your tests with json formatter, example: @RunWith(Cucumber.class)
@CucumberOptions(plugin = {"json:target/cucumber.json"} )
|
<plugin>
<groupId>com.github.cukedoctor</groupId>
<artifactId>cukedoctor-maven-plugin</artifactId>
<version>1.0.6</version>
<configuration>
<outputFileName>documentation</outputFileName> (1)
<outputDir>docs</outputDir> (2)
<format>pdf</format> (3)
<toc>left</toc> (4)
<numbered>true</numbered> (5)
<docVersion>${project.version}</docVersion> (6)
</configuration>
<executions>
<execution>
<goals>
<goal>execute</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
-
documentation filename
-
directory name (relative to /target) to generate documetation (default is cukedoctor)
-
document format, default is html5
-
table of content position, default is right
-
section numbering, default is false
-
documentation version (asciidoctor revNumber)
ℹ️
|
You can also execute the plugin without building the project but make sure you already have cucumber json files in build dir. mvn cukedoctor:execute |
Name | Description | Default |
---|---|---|
|
Generated documentation file name |
documentation |
|
Directory of where documentation will be saved |
${buildDir}/cukedoctor |
|
Documentation title (first section) |
Living Documentation |
|
Generated documetation format. Possible values: pdf, html, all |
html |
|
Documentarion version |
|
|
highlighter for source code rendering |
highlightjs (coderay is also supported) |
|
Table of contents position |
right |
|
Section numbering |
true |
|
Directory to start searching (recursively) for cucumber json output |
project root directory |
|
Flag to disable filter |
|
|
Flag to disable minimizable feature sections |
|
|
Flag to disable theme support |
|
|
When present, this flag hides |
|
|
When present, this flag hides |
|
|
When present, this flag |
|
|
When present, this flag hides |
|
|
When present, this flag hides |
You can disable Cukedoctor extensions using the following configuration in maven plugin:
<configuration>
<outputFileName>documentation</outputFileName> (1)
<outputDir>docs</outputDir>
<format>all</format>
<toc>left</toc> (4)
<disableTheme>true</disableTheme>
<disableFilter>true</disableFilter>
<disableMinimizable>true</disableMinimizable>
<disableStyle>true</disableStyle>
</configuration>
ℹ️
|
The value doesn’t matter, if there is something in the attribute the extension will be disabled |
This module brings the ability to execute cukedoctor converter as a Java main application (using command line: java -jar).
To use Cukedoctor as a standalone jar you can download it here.
This module converts generated adoc files into html and pdf, here’s an example:
@Test
public void shouldRenderHtmlForOneFeature(){
CukedoctorMain main = new CukedoctorMain();
main.execute(new String[]{
"-o", "\"target/document-one\"", (1)
"-p", "\"target/test-classes/json-output/one_passing_one_failing.json\"", (2)
"-t", "Living Documentation", (3)
"-f", "html", (4)
"-toc", "left", (5)
"-numbering", "true", (6)
"-sourceHighlighter", "coderay" (7)
});
File generatedFile = FileUtil.loadFile("target/document-one.html");
assertThat(generatedFile).exists();
}
-
output file name (default is 'documentation')
-
path to cucumber json files or directory (default is current dir - the search is recursive)
-
Document title (default is 'Living Documentation')
-
document format (Default is html)
-
table of contents position (Default is right)
-
Section numbering (Default is false)
-
Source highlighter (Default is highlightjs)
Using in command line, the above test should be something like:
java -jar cukedoctor-main.jar
-o "target/document-one"
-p "target/test-classes/json-output/one_passing_one_failing.json"
-t "Living Documentation" -f html
-hideSummarySection
-hideScenarioKeyword
You can use maven exec plugin, see example:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>com.github.cukedoctor.cukedoctorMain</argument>
</arguments>
</configuration>
</plugin>
❗
|
cukedoctor-main must be on your classpath |
To invoke Cukedoctor just use:
mvn exec:exec
It will run with default args. To provide arguments, in this approach, you’ll have a bit more work: see here.
You can disable cukedoctor extensions by using -D option when executing Cukedoctor main at command line:
java -jar -Dcukedoctor.disable.filter=123 -Dcukedoctor.disable.theme=abc
cukedoctor-main-1.0.6.jar
-p cucumber-output.json
You can download Cukedoctor main jar here
Some pieces of documentation can be hidden via configuration.
You can hide Features
and Summary
sections, as well as scenario keyword
which prefixes each scenario and hide tags
or step time
. To do so just specify the following arg parameters respectively:
java -jar -Dcukedoctor.disable.filter=123 -Dcukedoctor.disable.theme=abc
cukedoctor-main-1.0.6.jar
-p cucumber-output.json
-hideFeaturesSection (1)
-hideSummarySection (2)
-hideScenarioKeyword (3)
-hideStepTime (4)
-hideTags (5)
-
Removes
Features
section so each feature is a section instead of a sub section ofFeatures
; -
Removes summary section
-
Removes
scenario
keyword which prefixes each scenario; -
Removes step time calculation on each step;
-
Removes tags rendering;
Cukedoctor extension adds new features to generated documentation in order to let original document cleaner and make it easier to enable/disable those features.
This module extend cukedoctor generated documentation via Asciidoctor extensions mechanism.
Cukedoctor comes with 5 extensions to enhance documentation content:
-
Filter extension which lets features to be filtered using an input at top right of the page;
-
Minimizable extension which lets you minimize/maximize features sections (minus/plus icon next to feature name);
-
Theme extension to add theme support.
-
Footer add cukedoctor footer.
-
Style customizes Asciidoctor stylesheet.
ℹ️
|
All extensions target html documentation. |
To disable extensions just set the following system properties:
System.setProperty("cukedoctor.disable.theme","anyValue");
System.setProperty("cukedoctor.disable.filter","anyValue");
System.setProperty("cukedoctor.disable.minmax","anyValue");
System.setProperty("cukedoctor.disable.footer","anyValue");
System.setProperty("cukedoctor.disable.style","anyValue");
ℹ️
|
The value doesn’t matter, if there is something in the system property the extension will be disabled. |
💡
|
You can re-enable the extensions by calling System.clearProperty("cukedoctor.disable.theme");
System.clearProperty("cukedoctor.disable.filter");
System.clearProperty("cukedoctor.disable.minmax");
System.clearProperty("cukedoctor.disable.footer");
System.clearProperty("cukedoctor.disable.style"); |
Cukedoctor brings Living documentation to Jenkins via Cucumber living documentation plugin.
Cukedoctor is available at Bintray and at Maven central.
Snapshots are available at maven central and published on each successful commit&build on travis.
You can use snapshots by adding the following snippets in pom.xml:
<repositories>
<repository>
<snapshots/>
<id>snapshots</id>
<name>libs-snapshot</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
💡
|
You can download snapshots directly from Sonatype here. |
-
Found a bug? open an issue and attach your feature json output to it;
-
Have an idea? open an issue and lets discuss it;
-
Any form of feedback is more than welcome!