1 //! Module providing interface for running tests in the console.
5 use std::io::prelude::Write;
10 bench::fmt_bench_samples,
12 event::{CompletedTest, TestEvent},
14 formatters::{JsonFormatter, OutputFormatter, PrettyFormatter, TerseFormatter},
15 helpers::{concurrency::get_concurrency, metrics::MetricMap},
16 options::{Options, OutputFormat},
18 test_result::TestResult,
20 types::{NamePadding, TestDesc, TestDescAndFn},
23 /// Generic wrapper over stdout.
24 pub enum OutputLocation<T> {
25 Pretty(Box<term::StdoutTerminal>),
29 impl<T: Write> Write for OutputLocation<T> {
30 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
32 OutputLocation::Pretty(ref mut term) => term.write(buf),
33 OutputLocation::Raw(ref mut stdout) => stdout.write(buf),
37 fn flush(&mut self) -> io::Result<()> {
39 OutputLocation::Pretty(ref mut term) => term.flush(),
40 OutputLocation::Raw(ref mut stdout) => stdout.flush(),
45 pub struct ConsoleTestState {
46 pub log_out: Option<File>,
51 pub allowed_fail: usize,
52 pub filtered_out: usize,
54 pub metrics: MetricMap,
55 pub failures: Vec<(TestDesc, Vec<u8>)>,
56 pub not_failures: Vec<(TestDesc, Vec<u8>)>,
57 pub time_failures: Vec<(TestDesc, Vec<u8>)>,
61 impl ConsoleTestState {
62 pub fn new(opts: &TestOpts) -> io::Result<ConsoleTestState> {
63 let log_out = match opts.logfile {
64 Some(ref path) => Some(File::create(path)?),
77 metrics: MetricMap::new(),
79 not_failures: Vec::new(),
80 time_failures: Vec::new(),
81 options: opts.options,
85 pub fn write_log<F, S>(&mut self, msg: F) -> io::Result<()>
94 let msg = msg.as_ref();
95 o.write_all(msg.as_bytes())
100 pub fn write_log_result(
104 exec_time: Option<&TestExecTime>,
105 ) -> io::Result<()> {
110 TestResult::TrOk => "ok".to_owned(),
111 TestResult::TrFailed => "failed".to_owned(),
112 TestResult::TrFailedMsg(ref msg) => format!("failed: {}", msg),
113 TestResult::TrIgnored => "ignored".to_owned(),
114 TestResult::TrAllowedFail => "failed (allowed)".to_owned(),
115 TestResult::TrBench(ref bs) => fmt_bench_samples(bs),
116 TestResult::TrTimedFail => "failed (time limit exceeded)".to_owned(),
121 if let Some(exec_time) = exec_time {
122 self.write_log(|| format!(" <{}>", exec_time))?;
124 self.write_log(|| "\n")
127 fn current_test_count(&self) -> usize {
128 self.passed + self.failed + self.ignored + self.measured + self.allowed_fail
132 // List the tests to console, and optionally to logfile. Filters are honored.
133 pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<()> {
134 let mut output = match term::stdout() {
135 None => OutputLocation::Raw(io::stdout()),
136 Some(t) => OutputLocation::Pretty(t),
139 let quiet = opts.format == OutputFormat::Terse;
140 let mut st = ConsoleTestState::new(opts)?;
145 for test in filter_tests(&opts, tests) {
146 use crate::TestFn::*;
148 let TestDescAndFn { desc: TestDesc { name, .. }, testfn } = test;
150 let fntype = match testfn {
151 StaticTestFn(..) | DynTestFn(..) => {
155 StaticBenchFn(..) | DynBenchFn(..) => {
161 writeln!(output, "{}: {}", name, fntype)?;
162 st.write_log(|| format!("{} {}\n", fntype, name))?;
165 fn plural(count: u32, s: &str) -> String {
167 1 => format!("{} {}", 1, s),
168 n => format!("{} {}s", n, s),
173 if ntest != 0 || nbench != 0 {
174 writeln!(output, "")?;
177 writeln!(output, "{}, {}", plural(ntest, "test"), plural(nbench, "benchmark"))?;
183 // Updates `ConsoleTestState` depending on result of the test execution.
184 fn handle_test_result(st: &mut ConsoleTestState, completed_test: CompletedTest) {
185 let test = completed_test.desc;
186 let stdout = completed_test.stdout;
187 match completed_test.result {
188 TestResult::TrOk => {
190 st.not_failures.push((test, stdout));
192 TestResult::TrIgnored => st.ignored += 1,
193 TestResult::TrAllowedFail => st.allowed_fail += 1,
194 TestResult::TrBench(bs) => {
195 st.metrics.insert_metric(
196 test.name.as_slice(),
197 bs.ns_iter_summ.median,
198 bs.ns_iter_summ.max - bs.ns_iter_summ.min,
202 TestResult::TrFailed => {
204 st.failures.push((test, stdout));
206 TestResult::TrFailedMsg(msg) => {
208 let mut stdout = stdout;
209 stdout.extend_from_slice(format!("note: {}", msg).as_bytes());
210 st.failures.push((test, stdout));
212 TestResult::TrTimedFail => {
214 st.time_failures.push((test, stdout));
219 // Handler for events that occur during test execution.
220 // It is provided as a callback to the `run_tests` function.
223 st: &mut ConsoleTestState,
224 out: &mut dyn OutputFormatter,
225 ) -> io::Result<()> {
226 match (*event).clone() {
227 TestEvent::TeFiltered(ref filtered_tests) => {
228 st.total = filtered_tests.len();
229 out.write_run_start(filtered_tests.len())?;
231 TestEvent::TeFilteredOut(filtered_out) => {
232 st.filtered_out = filtered_out;
234 TestEvent::TeWait(ref test) => out.write_test_start(test)?,
235 TestEvent::TeTimeout(ref test) => out.write_timeout(test)?,
236 TestEvent::TeResult(completed_test) => {
237 let test = &completed_test.desc;
238 let result = &completed_test.result;
239 let exec_time = &completed_test.exec_time;
240 let stdout = &completed_test.stdout;
242 st.write_log_result(test, result, exec_time.as_ref())?;
243 out.write_result(test, result, exec_time.as_ref(), &*stdout, st)?;
244 handle_test_result(st, completed_test);
251 /// A simple console test runner.
252 /// Runs provided tests reporting process and results to the stdout.
253 pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<bool> {
254 let output = match term::stdout() {
255 None => OutputLocation::Raw(io::stdout()),
256 Some(t) => OutputLocation::Pretty(t),
259 let max_name_len = tests
261 .max_by_key(|t| len_if_padded(*t))
262 .map(|t| t.desc.name.as_slice().len())
265 let is_multithreaded = opts.test_threads.unwrap_or_else(get_concurrency) > 1;
267 let mut out: Box<dyn OutputFormatter> = match opts.format {
268 OutputFormat::Pretty => Box::new(PrettyFormatter::new(
275 OutputFormat::Terse => {
276 Box::new(TerseFormatter::new(output, opts.use_color(), max_name_len, is_multithreaded))
278 OutputFormat::Json => Box::new(JsonFormatter::new(output)),
280 let mut st = ConsoleTestState::new(opts)?;
282 run_tests(opts, tests, |x| on_test_event(&x, &mut st, &mut *out))?;
284 assert!(st.current_test_count() == st.total);
286 out.write_run_finish(&st)
289 // Calculates padding for given test description.
290 fn len_if_padded(t: &TestDescAndFn) -> usize {
291 match t.testfn.padding() {
292 NamePadding::PadNone => 0,
293 NamePadding::PadOnRight => t.desc.name.as_slice().len(),