How to add test to a simple library

Hi

I'm discovering rust and I try to make a very simple library.

My library code build: 'cargo build' command produce a dll file, but I'm struggling adding some tests to my project because I don't understant how to use the object I want to test.

I've made a simple project to illustrate my problem.
Would someone be kind enough to explain me how to solve this ?
Don't be afraid to state the obvious, I literaly started learning about rust yesterday.

Here are the files from my sample project

libdummy/Cargo.toml

[package]

name = "libdummy"

version = "0.1.0"

edition = "2021"

[lib]

crate-type = ["dylib"]

[dependencies]

src/lib.rs
this file is empty and I don't really undertand its purpose. Is it something intended to add some library initialisation code (like DllMain) ?

src/dummy.rs

#[derive(Debug)]
pub struct Dummy {
data: u128,
}

impl Dummy {
pub fn new() -> Self {
Self { data: 0 }
}

pub fn get(&self) -> u128 {
    self.data
}

pub fn set(&mut self, data: u128) {
    self.data = data;
}

}

tests/test_dummy.rs

#[cfg(test)]
mod tests {

#[test]
fn test_dummy() {
    let mut dum: Dummy = Dummy::new();
    assert!(0 == dum.get());
    dum.set(42);
    assert!(42 == dum.get());
}

}

when invoking cargo test I get the following error

Compiling libdummy v0.1.0 (...\libdummy)
error[E0412]: cannot find type Dummy in this scope
--> tests\test_dummy.rs:6:22
|
6 | let mut dum: Dummy = Dummy::new();
| ^^^^^ not found in this scope

error[E0433]: failed to resolve: use of undeclared type Dummy
--> tests\test_dummy.rs:6:30
|
6 | let mut dum: Dummy = Dummy::new();
| ^^^^^ use of undeclared type Dummy

Some errors have detailed explanations: E0412, E0433.
For more information about an error, try rustc --explain E0412.
error: could not compile libdummy (test "test_dummy") due to 2 previous errors

I understand that I must explain what "Dummy" and that I want to use it in this test code, but I don't understand how to express it.

Whenever you write unit tests and you want to access something from the module, do this:

#[cfg(test)]
mod tests {
    use super::*;

If that is the case your library contains nothing. You need to declare and export your items in there first.

Then in your test, import the exported item using its full path.

2 Likes

Thank you
I still got some errors with cargo test

error[E0412]: cannot find type Dummy in this scope
--> tests\test_dummy.rs:6:22
|
6 | let mut dum: Dummy = Dummy::new();
| ^^^^^ not found in this scope

warning: unused import: super::*
--> tests\test_dummy.rs:3:9
|
3 | use super::*;
| ^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

error[E0433]: failed to resolve: use of undeclared type Dummy
--> tests\test_dummy.rs:6:30
|
6 | let mut dum: Dummy = Dummy::new();
| ^^^^^ use of undeclared type Dummy

I guess I should add something in lib.rs like erelde said

Tests in tests/ directory treat your library like an external dependency. Your library is not special in any way. Your public symbols are not automatic globals. You have to refer to items in your library as if it was somebody else's crate.

Put that in your test modules:

use dummy::Dummy
1 Like

Ah, yes. lib.rs is the default library file. You put there whatever you want to expose in your library.

So now I have

src/lib.rs

pub mod dummy;

src/dummy.rs

#[derive(Debug)]
pub struct Dummy {
data: u128,
}

impl Dummy {
pub fn new() -> Self {
Self { data: 0 }
}

pub fn get(&self) -> u128 {
    self.data
}

pub fn set(&mut self, data: u128) {
    self.data = data;
}

}

tests/test_dummy.rs

use libdummy::dummy::Dummy;

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dummy() {
let mut dum: Dummy = Dummy::new();
assert!(0 == dum.get());
dum.set(42);
assert!(42 == dum.get());
}
}

Cargo.toml

[package]

name = "libdummy"

version = "0.1.0"

edition = "2021"

[lib]

crate-type = ["dylib"]

[dependencies]

cargo build is ok

cargo test fail

error: cannot satisfy dependencies so std only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so core only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so compiler_builtins only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so rustc_std_workspace_core only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so alloc only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so libc only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so unwind only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so cfg_if only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so miniz_oxide only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so adler only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so hashbrown only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so rustc_std_workspace_alloc only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so std_detect only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so rustc_demangle only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so panic_unwind only shows up once
|
= help: having upstream crates all available in one format will likely make this go away

error: could not compile libdummy (test "test_dummy") due to 15 previous errors

Check ths thread: Simple dylib linking fails. · Issue #19680 · rust-lang/rust · GitHub

1 Like

Thank you very much !

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.