-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcucumber.rs
79 lines (71 loc) · 2.12 KB
/
cucumber.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use cucumber::{then, when, StatsWriter, World};
use tracing::level_filters::LevelFilter;
use tracing::{error, span, Level};
use tracing_span_capture::{RecordedLogs, TracingSpanCaptureLayer};
use tracing_subscriber::fmt::format;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Layer;
#[derive(Debug, Default, World)]
struct LogsWorld {
last_error_message: Option<String>,
}
fn tested_function() {
error!("abc");
error!("emit test1");
}
#[when("Function is called")]
fn call_function(world: &mut LogsWorld) {
let span = span!(Level::INFO, "");
let record = RecordedLogs::new(&span);
{
let _enter = span.enter();
tested_function();
}
let logs = record.into_logs();
world.last_error_message = logs
.into_iter()
.rev()
.find(|e| e.level == Level::ERROR)
.map(|e| e.message);
}
#[then(expr = "Error log {string} is emitted")]
fn then_error(world: &mut LogsWorld, text: String) {
let last_error = world
.last_error_message
.as_ref()
.expect("no error was emitted");
assert!(
last_error.contains(&text),
"Expected log containing {text} got: {last_error}"
);
}
#[then(expr = "Error log {string} is not emitted")]
fn then_not_error(world: &mut LogsWorld, text: String) {
if let Some(last_error) = &world.last_error_message {
assert!(
!last_error.contains(&text),
"Expected log not containing {text} got: {last_error}"
);
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let summary = LogsWorld::cucumber()
.configure_and_init_tracing(
format::DefaultFields::new(),
format::Format::default(),
|layer| {
tracing_subscriber::registry()
.with(TracingSpanCaptureLayer)
.with(LevelFilter::INFO.and_then(layer))
},
)
.run("tests/features/")
.await;
assert!(!summary.execution_has_failed());
assert_eq!(
summary.scenarios_stats().skipped,
0,
"Should be no skipped scenarios"
);
}