Skip to content

Testing in Rust: test_runner placement #1453

@Opoodoop

Description

@Opoodoop

in the section Testing in Rust, the post talks about the following code, but does not make it clear that the first 2 lines (or entire code snippet) need to be placed before mod vga_buffer;

#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]

#[cfg(test)]
pub fn test_runner(tests: &[&dyn Fn()]) {
    println!("Running {} tests", tests.len());
    for test in tests {
        test();
    }
}

a person following this guide might place it anywhere in the file but making it clear that the following 2 options are of the few valid placements

  1. Placing it all at the start resulting in this as the first lines of code
#![no_std]
#![no_main]

#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]

#[cfg(test)]
pub fn test_runner(tests: &[&dyn Fn()]) {
    println!("Running {} tests", tests.len());
    for test in tests {
        test();
    }
}

mod vga_buffer;
...
  1. or placing the 2 attributes near the top of the file at the latest following the no_std and no_main attributes like this while placing the function elsewhere like this
#![no_std]
#![no_main]

#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]

mod vga_buffer;
...
#[cfg(test)]
pub fn test_runner(tests: &[&dyn Fn()]) {
    println!("Running {} tests", tests.len());
    for test in tests {
        test();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions