Working with the Rust book and failing to compile a simple integration test

Hello,

I am reading the Rust book How to Write Tests - The Rust Programming Language
and have this directory structure for a project named hello_cargo.
I get a compilation error that I wrote as comment above the code line. It cannot resolve the project name but this is what the Rust book does for the integration test, too.
Can someone explain why this does not work?
Test Organization - The Rust Programming Language

src/
- main.rs
- module1/
-- mod.rs
-- submodule1/
--- mod.rs
- module2/
-- mod.rs
- lib.rs
tests/
- integration_test.rs
// test/integration_test.rs

// compiler error:
// failed to resolve: use of unresolved module or unlinked crate `hello_cargo`
use hello_cargo::{module1, module2, module2::submodule1};

#[test]
fn testmain() {
    assert_eq!(main::module1::mod1string(), "mod1string");
    assert_eq!(main::module2::mod2string(), "mod2string");
    assert_eq!(main::module1::submodule1::mod1string(), "submod1string");
    //assert_eq!(main1::main1test(), "main1test");
}

You cannot access the functions of a binary crate (the one whose root file is src/main.rs) from outside it. As I wrote in your previous thread, the “integration test” feature of Cargo is intended to be used for two use cases:

  • Testing a library crate by using its public functions.
  • Testing a binary crate by running the program and seeing what it does.

Testing functions in the code of a binary crate is neither of those things — to write that kind of test, you must write the tests in the crate's own source code in src/, not in tests/. That's the only way the functions will be reachable.

Also, even if you could, the binary crate's name is not main, but hello_cargo (same as the library, confusingly); there is no crate or module named main. hello_cargo::... is the correct path to refer to the library (whose root file is src/lib.rs); main::... doesn't mean anything. (In general, lib.rs, main.rs, and mod.rs are names that are special and mean something other than the names of modules or crates.)

2 Likes

thanks now that you mention it I forgot about that.
I have another question - what is the difference from my project to the one in the rust book?
I only noticed that it does not have a main.rs crate and modules and submodules but the latter should not change the behaviour right? Can you tell me what difference it is that makes the example in the book compile but mine not? They also use a testsdirectory next to src with an ìntegration_test.rs` file in it.
I just tried to copy their project structure and experiment a bit.

adder
├── Cargo.lock
├── Cargo.toml
├── src
│   └── lib.rs
└── tests
    └── integration_test.rs
// file: tests/integration_test.rs
use adder::add_two;

#[test]
fn it_adds_two() {
    let result = add_two(2);
    assert_eq!(result, 4);
}

In that case the package has a library crate, only, and the tests are written to test the library crate.

If you added a binary crate by adding main.rs, then the integration test of the library would still work.

1 Like

oh wow I overlooked this - I was testing main.rs not lib.rs... thanks :slight_smile: