cucumber-rs / cucumber

Cucumber testing framework for Rust. Fully native, no external test runners or dependencies.
https://cucumber-rs.github.io/cucumber/main
Apache License 2.0
563 stars 69 forks source link

I can't get the JUnit XML report to compile; cannot infer type of the type parameter `W` declared on the struct `JUnit` #318

Closed henkoch closed 8 months ago

henkoch commented 8 months ago

Hi

How do I get the example code in [JUnit XML report](https://cucumber-rs.github.io/cucumber/main/output/junit.html#junit-xml-report)

let file = File::create(dbg!(format!("./junit.xml"))).expect("Failed to create file"); World::cucumber().with_writer(writer::JUnit::new(file, 0)).run("tests/features/book").await;

I interprest that the second line should look like this: World::cucumber().with_writer(writer::JUnit::<W, std::fs::File>::new(file, 0)).run("tests/features/book").await;

if that is the case what should 'W' be?

The code in the book example gives this compile output:

`Compiling edge_connector v0.1.0 (edge_connector) error[E0283]: type annotations needed --> tests/cucumber.rs:68:35 68 World::cucumber().with_writer(writer::JUnit::new(file, 0)).run("tests/features/book").await; --------------- ^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter 'W' declared on the struct 'JUnit'
type must be known at this point
= note: cannot satisfy '_: Debug'
note: required by a bound in 'cucumber' --> .cargo/registry/src/index.crates.io-6f17d22bba15001f/cucumber-0.20.2/src/lib.rs:267:15 265 fn cucumber<I: AsRef>() -> DefaultCucumber<Self, I> -------- required by a bound in this associated function 266 where 267 Self: Debug + WorldInventory, ^^^^^ required by this bound in 'World::cucumber' help: consider specifying the generic arguments
68 World::cucumber().with_writer(writer::JUnit::<W, File>::new(file, 0)).run("tests/features/book").await;
+++++++++++

For more information about this error, try 'rustc --explain E0283'. error: could not compile 'edge_connector' (test "cucumber") due to previous error`

OS: Ubuntu 22.04.3 LTS rustc: 1.73.0

Cargo.toml:

`[package] name = "edge_connector" version = "0.1.0" edition = "2021"

[dependencies] cucumber = { version = "0.20.2", features = ["output-junit"] } futures = "0.3.30" futures-channel = "0.3.30" futures-util = { version = "0.3.30", default-features = false, features = ["sink", "std"] } tokio = { version = "1.35.1", default-features = false, features = ["io-std", "macros", "rt-multi-thread", "time"] } tokio-tungstenite = "0.21.0" url = "2.5.0"

[[test]] name = "cucumber" path = "tests/cucumber.rs"`

ilslv commented 8 months ago

W should be your World type.

tyranron commented 8 months ago

@henkoch reading your error carefully, it seems that your World type misses Debug implementation, which is required:

error[E0283]: type annotations needed
   --> tests/cucumber.rs:68:35
    |
68  |     World::cucumber().with_writer(writer::JUnit::new(file, 0)).run("tests/features/book").await;
    |     ---------------               ^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter 'W' declared on the struct 'JUnit'
    |     |
    |     type must be known at this point
    |
    = note: cannot satisfy '_: Debug'
note: required by a bound in 'cucumber'
   --> .cargo/registry/src/index.crates.io-6f17d22bba15001f/cucumber-0.20.2/src/lib.rs:267:15
    |
265 |     fn cucumber<I: AsRef<Path>>() -> DefaultCucumber<Self, I>
    |        -------- required by a bound in this associated function
266 |     where
267 |         Self: Debug + WorldInventory,
    |               ^^^^^ required by this bound in 'World::cucumber'
help: consider specifying the generic arguments
    |
68  |     World::cucumber().with_writer(writer::JUnit::<W, File>::new(file, 0)).run("tests/features/book").await;
    |                                                +++++++++++

For more information about this error, try 'rustc --explain E0283'.
error: could not compile 'edge_connector' (test "cucumber") due to previous error

If you look at the full code in the example, it has the one:

Screenshot 2024-01-05 at 15 41 56
henkoch commented 8 months ago

Thank you both

This works let file = File::create(dbg!(format!("./junit.xml"))).expect("Failed to create file"); World::cucumber().with_writer(writer::JUnit::<WebSocketWorld, std::fs::File>::new(file, 1)).run("tests/features/book").await;

@tyranron I typed in your example, and that made me realize the I had created a structure called WebSocketWorld but I was using World::cucumber() to run this. So this also works :-) WebSocketWorld::cucumber().with_writer(writer::JUnit::new(file, 1)).run("tests/features/book").await;