candronikos
candronikos

Reputation: 169

Rust integration test can't `use` library

Working to include integration tests to my project but I'm unable to import the library. I thought the new rules would allow me to just write a use statement but it isn't going very well :)

The code below shows the related components. Isn't this supposed to be valid?

Cargo.toml

[package]
name = "myswankynewpackage"
version = "0.1.0"
authors = ["Me Myself <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

tests/tests.rs

use myswankynewpackage;
// Also tried extern crate
// extern crate myswankynewpackage;

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

    mod module{

        #[test]
        fn module_test() {
        }
    }
}

I get an error saying it can't find the crate

error[E0432]: unresolved import `myswankynewpackage`
 --> tests/tests.rs:1:5
  |
1 | use myswankynewpackage;
  |     ^^^^^^^^^^^^^^^^^^ no `myswankynewpackage` external crate

Upvotes: 2

Views: 2444

Answers (1)

Paul Molodowitch
Paul Molodowitch

Reputation: 1446

So, I noticed that OP says the actual Cargo.toml matches the given one "except [OP is] ... using a couple of libraries."

I think what's causing problems is the libraries - if you have libraries (with a different name), you need to use the name of the library in the use statement. ie:

Cargo.toml

[package]
name = "myswankynewpackage"
version = "0.1.0"
authors = ["Me Myself <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
tests/tests.rs

[lib]
name = "myswankynewlib"
path = "src/lib.rs"

tests/tests.rs

// WRONG!
// use myswankynewpackage;

// RIGHT!
use myswankynewlib;

...

I had a similar error message, and originally found this post when searching before I realized the issue. So even if this WASN'T the original poster's problem, perhaps this answer will help others...

Upvotes: 3

Related Questions