Skip to content

docs(code-style-guide): Add unit test function name rule #667

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 2 commits into from
Sep 19, 2024
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
62 changes: 61 additions & 1 deletion modules/contributor/pages/code-style-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ The usage of `thiserror` is considered invalid.
----
#[derive(Snafu)]
enum Error {
#[snafu(display("failed to read config file of user {user_name}"))]
#[snafu(display("failed to read config file of user {user_name:?}"))]
FileRead {
source: std::io::Error,
user_name: String,
Expand Down Expand Up @@ -595,3 +595,63 @@ let memory: CpuQuantity = "2".parse();
----

====

== Writing tests

=== Unit test function names

Function names of unit tests must not include a redundant `test` prefix or
suffix.

It results in the output of `cargo test` containing superfluous mentions of
"test", especially when the containing module is called `test`. For example:
`my_crate::test::test_valid`.

Instead, use an appropriate name to describe what is being tested. The previous
example could then become: `my_crate::test::parse_valid_api_version`.

[TIP.code-rule,caption=Examples of correct code for this rule]
====

[source,rust]
----
#[cfg(test)]
mod test {
use super::*;

#[test]
fn parse_valid_api_version() {
todo!()
}

#[test]
fn parse_invalid_api_version() {
todo!()
}
}
----

====

[WARNING.code-rule,caption=Examples of incorrect code for this rule]
====

[source,rust]
----
#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_valid() {
todo!()
}

#[test]
fn test_invalid() {
todo!()
}
}
----

====