Skip to content

Commit ab430cc

Browse files
authored
Implement outputting in Cucumber JSON format (#159, #127)
- impl `writer::Json` behind `output-json` Cargo feature
1 parent 2e937cb commit ab430cc

File tree

13 files changed

+1575
-6
lines changed

13 files changed

+1575
-6
lines changed

.github/workflows/ci.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ jobs:
6161
strategy:
6262
fail-fast: false
6363
matrix:
64-
feature: ["<none>", "macros", "timestamps", "output-junit"]
64+
feature:
65+
- <none>
66+
- macros
67+
- timestamps
68+
- output-json
69+
- output-junit
6570
runs-on: ubuntu-latest
6671
steps:
6772
- uses: actions/checkout@v2

CHANGELOG.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
1414
### Added
1515

1616
- Ability for step functions to return `Result`. ([#151])
17-
- Arbitrary output for `writer::Basic` ([#147])
18-
- `writer::JUnit` ([JUnit XML report][0110-1]) behind the `output-junit` feature flag ([#147])
17+
- Arbitrary output for `writer::Basic`. ([#147])
18+
- `writer::JUnit` ([JUnit XML report][0110-1]) behind the `output-junit` feature flag. ([#147])
19+
- `writer::Json` ([Cucumber JSON format][0110-2]) behind the `output-json` feature flag. ([#159])
1920

2021
[#147]: /../../pull/147
2122
[#151]: /../../pull/151
23+
[#159]: /../../pull/159
2224
[0110-1]: https://llg.cubic.org/docs/junit
25+
[0110-2]: https://github.com/cucumber/cucumber-json-schema
2326

2427

2528

Cargo.toml

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ repository = "https://github.com/cucumber-rs/cucumber"
1919
readme = "README.md"
2020
categories = ["asynchronous", "development-tools::testing"]
2121
keywords = ["cucumber", "testing", "bdd", "atdd", "async"]
22-
include = ["/src/", "/tests/junit.rs", "/tests/wait.rs", "/LICENSE-*", "/README.md", "/CHANGELOG.md"]
22+
include = ["/src/", "/tests/json.rs", "/tests/junit.rs", "/tests/wait.rs", "/LICENSE-*", "/README.md", "/CHANGELOG.md"]
2323

2424
[package.metadata.docs.rs]
2525
all-features = true
@@ -29,6 +29,8 @@ rustdoc-args = ["--cfg", "docsrs"]
2929
default = ["macros"]
3030
# Enables step attributes and auto-wiring.
3131
macros = ["cucumber-codegen", "inventory"]
32+
# Enables support for outputting in Cucumber JSON format.
33+
output-json = ["Inflector", "serde", "serde_json", "timestamps"]
3234
# Enables support for outputting JUnit XML report.
3335
output-junit = ["junit-report", "timestamps"]
3436
# Enables timestamps collecting for all events.
@@ -54,6 +56,11 @@ structopt = "0.3.25"
5456
cucumber-codegen = { version = "0.11.0-dev", path = "./codegen", optional = true }
5557
inventory = { version = "0.1.10", optional = true }
5658

59+
# "output-json" feature dependencies
60+
serde = { version = "1.0.103", features = ["derive"], optional = true }
61+
serde_json = { version = "1.0.18", optional = true }
62+
Inflector = { version = "0.11", default-features = false, optional = true }
63+
5764
# "output-junit" feature dependencies
5865
junit-report = { version = "0.7", optional = true }
5966

@@ -62,6 +69,11 @@ humantime = "2.1"
6269
tempfile = "3.2"
6370
tokio = { version = "1.12", features = ["macros", "rt-multi-thread", "time"] }
6471

72+
[[test]]
73+
name = "json"
74+
required-features = ["output-json"]
75+
harness = false
76+
6577
[[test]]
6678
name = "junit"
6779
required-features = ["output-junit"]

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ For more examples check out the Book ([current][1] | [edge][2]).
100100

101101
- `macros` (default): Enables step attributes and auto-wiring.
102102
- `timestamps`: Enables timestamps collecting for all [Cucumber] events.
103+
- `output-json` (implies `timestamps`): Enables support for outputting in [Cucumber JSON format].
103104
- `output-junit` (implies `timestamps`): Enables support for outputting [JUnit XML report].
104105

105106

@@ -132,6 +133,7 @@ at your option.
132133

133134

134135
[Cucumber]: https://cucumber.io
136+
[Cucumber JSON format]: https://github.com/cucumber/cucumber-json-schema
135137
[Gherkin]: https://cucumber.io/docs/gherkin/reference
136138
[JUnit XML report]: https://llg.cubic.org/docs/junit
137139

book/src/Features.md

+44
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,50 @@ World::cucumber()
474474

475475

476476

477+
## Cucumber JSON format output
478+
479+
Library provides an ability to output tests result in a [Cucumber JSON format].
480+
481+
Just enable `output-json` library feature in your `Cargo.toml`:
482+
```toml
483+
cucumber = { version = "0.11", features = ["output-json"] }
484+
```
485+
486+
And configure [Cucumber]'s output to `writer::Json`:
487+
```rust
488+
# use std::{convert::Infallible, fs, io};
489+
#
490+
# use async_trait::async_trait;
491+
# use cucumber::WorldInit;
492+
use cucumber::writer;
493+
494+
# #[derive(Debug, WorldInit)]
495+
# struct World;
496+
#
497+
# #[async_trait(?Send)]
498+
# impl cucumber::World for World {
499+
# type Error = Infallible;
500+
#
501+
# async fn new() -> Result<Self, Self::Error> {
502+
# Ok(World)
503+
# }
504+
# }
505+
#
506+
# #[tokio::main]
507+
# async fn main() -> io::Result<()> {
508+
let file = fs::File::create(dbg!(format!("{}/target/schema.json", env!("CARGO_MANIFEST_DIR"))))?;
509+
World::cucumber()
510+
.with_writer(writer::Json::new(file))
511+
.run("tests/features/book")
512+
.await;
513+
# Ok(())
514+
# }
515+
```
516+
517+
518+
519+
477520
[Cucumber]: https://cucumber.io
521+
[Cucumber JSON format]: https://github.com/cucumber/cucumber-json-schema
478522
[Gherkin]: https://cucumber.io/docs/gherkin
479523
[JUnit XML report]: https://llg.cubic.org/docs/junit

book/tests/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ publish = false
1111

1212
[dependencies]
1313
async-trait = "0.1"
14-
cucumber = { version = "0.11.0-dev", path = "../..", features = ["output-junit"] }
14+
cucumber = { version = "0.11.0-dev", path = "../..", features = ["output-json", "output-junit"] }
1515
futures = "0.3"
1616
skeptic = "0.13"
1717
tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] }

0 commit comments

Comments
 (0)