rvtest 0.1.0

A Next Level Testing Framework for Rust — BDD specs, property-based testing, parametrized tests, rich reporting, and code coverage
Documentation
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use std::fmt::Write;

use crate::core::{TestRun, TestStatus};

// ---------------------------------------------------------------------------
// Reporter trait
// ---------------------------------------------------------------------------

/// Renders a [`TestRun`] into a human- or machine-readable string.
pub trait TestReporter {
    /// Format the entire test run into a string.
    fn report(&self, run: &TestRun) -> String;
}

// ---------------------------------------------------------------------------
// PrettyReporter — colourful, human-friendly output
// ---------------------------------------------------------------------------

fn sep_line() -> String {
    "".repeat(58)
}

/// Human-readable reporter with optional colour and verbosity.
pub struct PrettyReporter {
    verbose: bool,
    use_colour: bool,
}

impl PrettyReporter {
    /// Create a new `PrettyReporter`.
    ///
    /// When `verbose` is `true`, passing tests are also listed (by default
    /// only failures are shown in detail).
    pub fn new(verbose: bool) -> Self {
        PrettyReporter { verbose, use_colour: true }
    }

    /// Enable or disable ANSI colour codes.
    pub fn colour(mut self, enabled: bool) -> Self {
        self.use_colour = enabled;
        self
    }
}

impl TestReporter for PrettyReporter {
    fn report(&self, run: &TestRun) -> String {
        let mut out = String::new();

        for suite in &run.suites {
            // Suite header with centered text
            let header = format!("  {}  ", suite.name);
            let pad = 58usize.saturating_sub(header.chars().count());
            let left = pad / 2;
            let right = pad - left;
            let mut line = String::new();
            for _ in 0..left { line.push(''); }
            line.push_str(&header);
            for _ in 0..right { line.push(''); }
            let _ = writeln!(out, "\n{}", line);

            for test in &suite.tests {
                if !self.verbose && test.status.is_passed() {
                    continue;
                }

                let (icon, label) = status_badge(&test.status, self.use_colour);
                let dur = format_duration(test.duration);

                let _ = writeln!(out, "  {icon} {label}  {}  {}", test.name, dur);

                if let TestStatus::Failed { ref reason, ref location } = test.status {
                    if let Some(loc) = location {
                        let loc_str = format_location(loc, self.use_colour);
                        let _ = writeln!(out, "         {}  {}", dim("at", self.use_colour), loc_str);
                    }
                    for line in reason.lines() {
                        let _ = writeln!(out, "         {}", line);
                    }
                }

                if let TestStatus::TimedOut { duration, ref location } = test.status {
                    let _ = writeln!(out, "         {} {duration:?}", dim("timed out after", self.use_colour));
                    if let Some(loc) = location {
                        let _ = writeln!(out, "         {}  {}", dim("at", self.use_colour), format_location(loc, self.use_colour));
                    }
                }
            }
        }

        // Summary
        let total = run.total();
        let passed = run.total_passed();
        let failed = run.total_failed();
        let skipped = run.total_skipped();
        let dur_s = run.duration.as_secs_f64();

        let _ = writeln!(out);
        let _ = writeln!(out, "{}", sep_line());

        let status_text = if run.success() {
            coloured("ok", "32", self.use_colour)
        } else {
            coloured("FAILED", "31", self.use_colour)
        };

        let passed_s = coloured_count(passed, "passed", "32", self.use_colour);
        let failed_s = coloured_count(failed, "failed", "31", self.use_colour);
        let skipped_s = coloured_count(skipped, "skipped", "33", self.use_colour);

        let _ = writeln!(
            out,
            "  {status_text}  {passed_s} · {failed_s} · {skipped_s}{total} total  │  {dur_s:.2}s",
        );

        out
    }
}

fn status_badge(status: &TestStatus, colour: bool) -> (String, String) {
    match status {
        TestStatus::Passed => (coloured("", "32", colour), coloured("PASS", "32", colour)),
        TestStatus::Failed { .. } => (coloured("", "31", colour), coloured("FAIL", "31", colour)),
        TestStatus::Skipped { .. } => (coloured("", "33", colour), coloured("SKIP", "33", colour)),
        TestStatus::TimedOut { .. } => (coloured("", "31", colour), coloured("TIMEOUT", "31", colour)),
    }
}

fn format_duration(d: std::time::Duration) -> String {
    let secs = d.as_secs_f64();
    if secs >= 1.0 {
        format!("{secs:.2}s")
    } else {
        format!("{:.1}ms", secs * 1000.0)
    }
}

fn format_location(loc: &crate::core::SourceLocation, colour: bool) -> String {
    let s = match loc.column {
        Some(col) => format!("{}:{}:{}", loc.file, loc.line, col),
        None => format!("{}:{}", loc.file, loc.line),
    };
    coloured(&s, "36", colour)
}

fn coloured(s: &str, code: &str, enabled: bool) -> String {
    if enabled {
        format!("\x1b[{code}m{s}\x1b[0m")
    } else {
        s.to_owned()
    }
}

fn dim(s: &str, enabled: bool) -> String {
    coloured(s, "2", enabled)
}

fn coloured_count(n: usize, label: &str, colour_code: &str, enabled: bool) -> String {
    format!("{} {}", coloured(&n.to_string(), colour_code, enabled), label)
}

// ---------------------------------------------------------------------------
// TapReporter — Test Anything Protocol
// ---------------------------------------------------------------------------

/// Reporter that emits TAP (Test Anything Protocol) output.
///
/// TAP is a simple line-based protocol widely used in the Perl and
/// JavaScript ecosystems and supported by many CI tools.
pub struct TapReporter;

impl TestReporter for TapReporter {
    fn report(&self, run: &TestRun) -> String {
        let mut out = String::new();

        let total = run.total();
        let _ = writeln!(out, "1..{total}");

        let mut index = 0;
        for suite in &run.suites {
            for test in &suite.tests {
                index += 1;
                let ok = if test.status.is_passed() { "ok" } else { "not ok" };
                let duration_ms = test.duration.as_secs_f64() * 1000.0;

                let _ = writeln!(out, "{ok} {index} - {} [{duration_ms:.1}ms]", test.name);

                if let TestStatus::Failed { ref reason, .. } = test.status {
                    for line in reason.lines() {
                        let _ = writeln!(out, "  {line}");
                    }
                }

                if let TestStatus::TimedOut { duration, .. } = test.status {
                    let _ = writeln!(out, "  # TIMEOUT after {duration:?}");
                }

                if let TestStatus::Skipped { ref reason } = test.status {
                    let reason = reason.as_deref().unwrap_or("no reason given");
                    let _ = writeln!(out, "  # SKIP {reason}");
                }
            }
        }

        out
    }
}

// ---------------------------------------------------------------------------
// JunitReporter — JUnit XML for CI integration
// ---------------------------------------------------------------------------

/// Reporter that emits JUnit-compatible XML.
///
/// This format is understood by Jenkins, GitLab CI, GitHub Actions, and
/// most other CI systems.
pub struct JunitReporter {
    suite_name: String,
}

impl JunitReporter {
    /// Create a new `JunitReporter`.
    pub fn new() -> Self {
        JunitReporter { suite_name: "rvtest".to_owned() }
    }

    /// Override the top-level suite name in the XML output.
    pub fn suite_name(mut self, name: &str) -> Self {
        self.suite_name = name.to_owned();
        self
    }
}

impl Default for JunitReporter {
    fn default() -> Self {
        Self::new()
    }
}

impl TestReporter for JunitReporter {
    fn report(&self, run: &TestRun) -> String {
        let mut out = String::new();
        let _ = writeln!(out, r#"<?xml version="1.0" encoding="UTF-8"?>"#);
        let _ = writeln!(
            out,
            r#"<testsuites name="{}" tests="{}" failures="{}" skipped="{}" time="{:.3}">"#,
            self.suite_name,
            run.total(),
            run.total_failed(),
            run.total_skipped(),
            run.duration.as_secs_f64(),
        );

        for suite in &run.suites {
            let _ = writeln!(
                out,
                r#"  <testsuite name="{}" tests="{}" failures="{}" skipped="{}" time="{:.3}">"#,
                escape_xml(&suite.name),
                suite.len(),
                suite.failed().count(),
                suite.skipped().count(),
                suite.duration.as_secs_f64(),
            );

            for test in &suite.tests {
                let classname = test.suite.as_deref().unwrap_or("root");
                let dur_s = test.duration.as_secs_f64();

                match &test.status {
                    TestStatus::Passed => {
                        let _ = writeln!(
                            out,
                            r#"    <testcase classname="{}" name="{}" time="{:.3}" />"#,
                            escape_xml(classname),
                            escape_xml(&test.name),
                            dur_s,
                        );
                    }
                    TestStatus::Failed { reason, .. } => {
                        let _ = writeln!(
                            out,
                            r#"    <testcase classname="{}" name="{}" time="{:.3}">"#,
                            escape_xml(classname),
                            escape_xml(&test.name),
                            dur_s,
                        );
                        let _ = writeln!(
                            out,
                            r#"      <failure message="{}" type="AssertionError"><![CDATA[{}]]></failure>"#,
                            escape_xml(reason),
                            reason,
                        );
                        let _ = writeln!(out, "    </testcase>");
                    }
                    TestStatus::Skipped { reason } => {
                        let msg = reason.as_deref().unwrap_or("skipped");
                        let _ = writeln!(
                            out,
                            r#"    <testcase classname="{}" name="{}" time="{:.3}">"#,
                            escape_xml(classname),
                            escape_xml(&test.name),
                            dur_s,
                        );
                        let _ = writeln!(
                            out,
                            r#"      <skipped message="{}" />"#,
                            escape_xml(msg),
                        );
                        let _ = writeln!(out, "    </testcase>");
                    }
                    TestStatus::TimedOut { duration: to, .. } => {
                        let _ = writeln!(
                            out,
                            r#"    <testcase classname="{}" name="{}" time="{:.3}">"#,
                            escape_xml(classname),
                            escape_xml(&test.name),
                            dur_s,
                        );
                        let _ = writeln!(
                            out,
                            r#"      <failure message="timed out after {:?}" type="TimeoutError" />"#,
                            to,
                        );
                        let _ = writeln!(out, "    </testcase>");
                    }
                }
            }

            let _ = writeln!(out, "  </testsuite>");
        }

        let _ = writeln!(out, "</testsuites>");
        out
    }
}

fn escape_xml(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

// ---------------------------------------------------------------------------
// JsonReporter — machine-readable JSON
// ---------------------------------------------------------------------------

/// Reporter that emits results as a JSON object.
pub struct JsonReporter;

impl TestReporter for JsonReporter {
    fn report(&self, run: &TestRun) -> String {
        let mut out = String::new();

        let success = if run.success() { "true" } else { "false" };
        out.push_str(&format!(
            r#"{{"success":{},"total":{},"passed":{},"failed":{},"skipped":{},"duration_secs":{:.3},"suites":["#,
            success,
            run.total(),
            run.total_passed(),
            run.total_failed(),
            run.total_skipped(),
            run.duration.as_secs_f64(),
        ));

        for (si, suite) in run.suites.iter().enumerate() {
            if si > 0 {
                out.push(',');
            }
            out.push_str(&format!(
                r#"{{"name":"{}","duration_secs":{:.3},"tests":["#,
                escape_json(&suite.name),
                suite.duration.as_secs_f64(),
            ));

            for (ti, test) in suite.tests.iter().enumerate() {
                if ti > 0 {
                    out.push(',');
                }
                let (status_str, reason) = match &test.status {
                    TestStatus::Passed => ("passed", None),
                    TestStatus::Failed { reason, .. } => ("failed", Some(reason.as_str())),
                    TestStatus::Skipped { reason } => ("skipped", reason.as_deref()),
                    TestStatus::TimedOut { .. } => ("timed_out", None),
                };

                out.push_str(&format!(
                    r#"{{"name":"{}","status":"{}","duration_secs":{:.3}"#,
                    escape_json(&test.name),
                    status_str,
                    test.duration.as_secs_f64(),
                ));

                if let Some(r) = reason {
                    out.push_str(&format!(r#","reason":"{}""#, escape_json(r)));
                }

                if !test.parameters.is_empty() {
                    out.push_str(r#","parameters":{"#);
                    for (pi, (k, v)) in test.parameters.iter().enumerate() {
                        if pi > 0 {
                            out.push(',');
                        }
                        out.push_str(&format!(r#""{}":"{}""#, escape_json(k), escape_json(v)));
                    }
                    out.push('}');
                }

                out.push_str("]}");
            }

            out.push('}');
        }

        out.push(']');
        out.push('}');
        out
    }
}

fn escape_json(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

// ---------------------------------------------------------------------------
// CompactReporter — one line per test
// ---------------------------------------------------------------------------

/// Minimal, single-line-per-test reporter suitable for quick feedback.
pub struct CompactReporter;

impl TestReporter for CompactReporter {
    fn report(&self, run: &TestRun) -> String {
        let mut out = String::new();
        let total = run.total();
        let passed = run.total_passed();
        let failed = run.total_failed();
        let skipped = run.total_skipped();

        for suite in &run.suites {
            for test in &suite.tests {
                let status = match test.status {
                    TestStatus::Passed => "PASS",
                    TestStatus::Failed { .. } => "FAIL",
                    TestStatus::Skipped { .. } => "SKIP",
                    TestStatus::TimedOut { .. } => "TIMEOUT",
                };
                let dur = format_duration(test.duration);
                let _ = writeln!(out, "{status}  {dur:>7}  {}", test.name);
            }
        }

        let _ = writeln!(
            out,
            "\nResults: {passed}/{total} passed, {failed} failed, {skipped} skipped  ({:.2}s)",
            run.duration.as_secs_f64(),
        );

        out
    }
}