Crates.io | mt-mock-logger |
lib.rs | mt-mock-logger |
version | 0.1.2 |
source | src |
created_at | 2024-09-23 19:00:03.195958 |
updated_at | 2024-09-24 00:59:40.25471 |
description | A thread-safe mock logger for multiple tests running in parallel |
homepage | |
repository | https://github.com/diogomattioli/mt_mock_logger |
max_upload_size | |
id | 1384374 |
size | 7,698 |
A thread-safe mock logger designed to support mocking across multiple tests running in parallel.
The injected logger is automatically scoped to the lifetime of the returned MockLoggerGuard
provided by the set_logger
function.
The MockLogger
is not intended for use with async. Since it binds the injected logger to the test's ThreadId
, using it in an async context may result in unexpected behavior.
[dev-dependencies]
mt-mock-logger = "0.1"
#[cfg(test)]
mod tests {
use mt_mock_logger::MockLogger;
use mockall::mock;
use super::*;
mock! {
pub MyLogger {}
impl log::Log for MyLogger {
fn enabled<'a>(&self, metadata: &log::Metadata<'a>) -> bool;
fn log<'a>(&self, record: &log::Record<'a>);
fn flush(&self);
}
}
#[test]
fn test_logging() {
let mut my_logger = MockMyLogger::new();
my_logger
.expect_log()
.withf(|r| r.level() == log::LevelFilter::Info &&
r.args().as_str() == Some("ok"))
.once()
.return_const(());
let _guard = MockLogger::set_logger(my_logger, log::LevelFilter::Info);
log::info!("ok");
log::trace!("ok");
}
#[test]
fn test_logging_below_max_level() {
let mut my_logger = MockMyLogger::new();
my_logger.expect_log().never().return_const(());
let _guard = MockLogger::set_logger(my_logger, log::LevelFilter::Info);
log::trace!("ok");
}
#[test]
fn test_no_logger() {
log::trace!("ok");
}
}