Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

What is the purpose of grouping the tests in a `tests` module and is it possible to split them?

+7
−0

What is the purpose of grouping the tests in a tests module like this

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

    #[test]
    fn test_function_1() {
        // test code for function 1 goes here
    }

    #[test]
    fn test_function_2() {
        // test code for function 2 goes here
    }
}

and is it possible to split them and have the test functions outside of the tests module?

#[test]
fn test_function_1() {
    // test code for function 1 goes here
}

#[test]
fn test_function_2() {
    // test code for function 2 goes here
}

I don't want to split them into their own directory.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

3 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+9
−0

Grouping related items into modules is of course generally good practice, but it serves a practical purpose as well. The important part here is the #[cfg(test)] annotation.

The #[cfg(test)] annotation on the tests module tells Rust to compile and run the test code only when you run cargo test, not when you run cargo build. This saves compile time when you only want to build the library and saves space in the resulting compiled artifact because the tests are not included. You’ll see that because integration tests go in a different directory, they don’t need the #[cfg(test)] annotation. However, because unit tests go in the same files as the code, you’ll use #[cfg(test)] to specify that they shouldn’t be included in the compiled result.

As it says, it is just a way to separate tests from the library itself; the other way is to use a separate directory.

Creating tests outside of such a module actually does work, and as cafce25‭'s answer mentions, #[test] already implies #[cfg(test)]. However, you might also have helper functions or mocks for your tests that you also don't want to include in your final non-test binary. By putting them all into one module, you not only group them semantically, but this also allows you to #[cfg(test)] all of them in one place rather than adding it to each of them individually.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Can the module be split in the same file? (2 comments)
+5
−0

The reason to put tests in a separate module is so they are grouped.

Another advantage is that you can put helper functions that are only needed for tests, but are not themselves tests in the module as well:

#[cfg(test)]
mod tests {
    fn setup() -> Vec<i32> {
        vec![1, 2, 3]
    }

    #[test]
    fn test_length() {
        let v = setup();
        assert_eq!(v.len(), 3);
    }

    #[test]
    fn test_last() {
        let mut v = setup();
        assert_eq!(v.pop(), Some(3));
    }
}

It's not used to exclude test functions from the final binary, a #[test] annotation on it's own is sufficient to ensure that, i.e. it includes a #[cfg(test)] annotation. As specified in the reference

[#[test]] functions are only compiled when in test mode.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

When you think about how to organize your test code, you should develop an understanding of your goals. Typical goals about test code organization (which typically includes helper code only needed for the tests) are:

  • Ensure that test code does not become part of production code - for some kinds of software this can even be a strict requirement (for example in safety critical software) - even if the test code within the production code would be dead code or unreachable.

  • Ensure that in deliveries to the customer only the agreed parts are included, for example production code without tests or vice versa - depending on the contract.

  • Make it easily possible to handle test code differently in tools (for example, test coverage tools, static code analysis tools), for example by excluding them by path or module name or the like. The possibility to exclude them during normal compilation (which the #[cfg(test)] annotation achieves, as was mentioned by @Moshi) is just one example for this.

  • Especially in contexts where different kinds of tests have to be performed (unit-tests, integration tests, performance tests, ...) it may become a practical necessity to have the different kinds of code clearly separated and individually structured to avoid confusion even during development. You may need to make additional distinctions between short running and long running tests and so on.

By grouping tests into a separated module or directory etc. you can fulfill the aforementioned goals - in some cases you will even have to go beyond that and introduce further substructuring.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »