Testing folder modules

I am trying to figure out how to use modules in testing.

I have the following root folders



in player-tests.rs I want to import the src/player.rs struct and impl for testing
how can I keep my tests in the test folder but reference files in the src directory for proper unit testing?

Note: Files in the tests folder are integration tests which can only test your library's public API. If your tests need to use private types or functions from the player module, you'll need a unit test module that is a submodule of player, which can live either in src/player.rs or a separate file src/player/tests.rs.

For your integration tests, you can access your public API through your library crate, like this:

// tests/player-tests.rs

// Replace `my_crate_name` with the actual name of your Cargo project:
use my_crate_name::player::some_function;

fn test_something() {
    assert_eq!(some_function(), 0);

To create unit tests for your private API, you can create a test module like this:

// src/player.rs

mod tests;

The code in the test module will look something like this:

// src/player/tests.rs

use super::*; // Imports all items from the `player` module.

fn test_something() {
    // Works even if `player` or `player::some_function` is private:
    assert_eq!(some_function(), 0);

See the book for more details.

Thank you for your swift reply, much appreciated

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.