Skip to content

Suggest using mod.rs pattern to share test code #1577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/testing/integration_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
```

Each Rust source file in the `tests` directory is compiled as a separate crate. One
way of sharing some code between integration tests is making a module with public
Each Rust source file in the `tests` directory is compiled as a separate crate. In
order to share some code between integration tests we can make a module with public
functions, importing and using it within tests.

File `tests/common.rs`:
File `tests/common/mod.rs`:

```rust,ignore
pub fn setup() {
Expand All @@ -74,8 +74,9 @@ fn test_add() {
}
```

Modules with common code follow the ordinary [modules][mod] rules, so it's ok to
create common module as `tests/common/mod.rs`.
Creating the module as `tests/common.rs` also works, but is not recommended
because the test runner will treat the file as a test crate and try to run tests
inside it.

[unit]: unit_testing.md
[mod]: ../mod.md