use tokio::runtime::Builder;
use tracing::{
debug,
error,
info,
instrument,
};
mod something {
pub type Error = String;
}
use something::Error;
#[rstest_log::rstest]
fn without_return_type() {
assert_eq!(2 + 2, 4);
}
#[rstest_log::rstest]
fn with_return_type() -> Result<(), Error> {
Ok(())
}
#[rstest_log::rstest]
#[should_panic(expected = "success")]
fn with_panic() {
panic!("success")
}
#[rstest_log::rstest(tokio::test)]
async fn with_inner_test_attribute_and_async() {
assert_eq!(async { 42 }.await, 42)
}
#[rstest_log::rstest]
#[case(-2, -4)]
fn with_inner_test_attribute_and_test_args(#[case] x: i8, #[case] y: i8) {
assert_eq!(x, -2);
assert_eq!(y, -4);
}
#[rstest_log::rstest]
#[case::my_test_name(-2, -4)]
fn with_inner_test_attribute_and_test_args_and_name(#[case] x: i8, #[case] y: i8) {
assert_eq!(x, -2);
assert_eq!(y, -4);
}
#[should_panic]
#[rstest_log::rstest]
#[case(-2, -4)]
fn with_inner_test_attribute_and_test_args_and_panic(#[case] x: i8, #[case] _y: i8) {
assert_eq!(x, 0);
}
#[rstest_log::rstest(tokio::test)]
#[case(-2, 4)]
async fn async_case(#[case] x: i8, #[case] y: i8) {
assert_eq!(x, -2);
assert_eq!(y, 4);
}
#[instrument]
async fn instrumented(input: usize) -> usize {
info!("input = {}", input);
if input == 0 || input == 4 {
error!("here we go");
}
let result = input + 1;
info!("result = {}", result);
result
}
#[rstest_log::rstest]
fn trace_with_custom_runtime() {
let rt = Builder::new_current_thread().build().unwrap();
rt.block_on(async {
instrumented(0).await;
instrumented(1).await;
debug!("done");
})
}
#[rstest_log::rstest(tokio::test)]
async fn trace_with_tokio_attribute() {
instrumented(6).await;
instrumented(4).await;
debug!("done");
}
#[rstest_log::rstest(tokio::test(flavor = "multi_thread", worker_threads = 1))]
async fn trace_with_tokio_attribute_with_arguments() {
instrumented(6).await;
instrumented(4).await;
debug!("done");
}
mod local {
use rstest_log::rstest;
use super::Error;
#[rstest]
fn without_return_type() {
assert_eq!(2 + 2, 4);
}
#[rstest]
fn with_return_type() -> Result<(), Error> {
Ok(())
}
#[rstest]
#[should_panic(expected = "success")]
fn with_panic() {
panic!("success")
}
#[rstest(tokio::test)]
async fn with_inner_test_attribute_and_async() {
assert_eq!(async { 42 }.await, 42)
}
#[rstest]
#[case(-2, -4)]
fn with_inner_test_attribute_and_test_args(#[case] x: i8, #[case] y: i8) {
assert_eq!(x, -2);
assert_eq!(y, -4);
}
#[should_panic]
#[rstest]
#[case(-2, -4)]
fn with_inner_test_attribute_and_test_args_and_panic(#[case] x: i8, #[case] _y: i8) {
assert_eq!(x, 0);
}
}