Skip to content

Commit db164f4

Browse files
committed
docs(code-style-guide): Add unit test function name rule
1 parent 413528d commit db164f4

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

modules/contributor/pages/code-style-guide.adoc

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ The usage of `thiserror` is considered invalid.
296296
----
297297
#[derive(Snafu)]
298298
enum Error {
299-
#[snafu(display("failed to read config file of user {user_name}"))]
299+
#[snafu(display("failed to read config file of user {user_name:?}"))]
300300
FileRead {
301301
source: std::io::Error,
302302
user_name: String,
@@ -595,3 +595,57 @@ let memory: CpuQuantity = "2".parse();
595595
----
596596
597597
====
598+
599+
== Writing tests
600+
601+
=== Unit test function names
602+
603+
Function names of unit tests must not include the `test_` prefix, because otherwise `cargo test` will produce superfluous output like `memory::test::test_addition::case_1`.
604+
Instead, use an appropriate name to describe what is being tested.
605+
The previously mentioned name could for example be renamed to `memory::test::addition::case_1`.
606+
607+
[TIP.code-rule,caption=Examples of correct code for this rule]
608+
====
609+
610+
[source,rust]
611+
----
612+
#[cfg(test)]
613+
mod test {
614+
use super::*;
615+
616+
#[test]
617+
fn parse_valid_api_version() {
618+
todo!()
619+
}
620+
621+
#[test]
622+
fn parse_invalid_api_version() {
623+
todo!()
624+
}
625+
}
626+
----
627+
628+
====
629+
630+
[WARNING.code-rule,caption=Examples of incorrect code for this rule]
631+
====
632+
633+
[source,rust]
634+
----
635+
#[cfg(test)]
636+
mod test {
637+
use super::*;
638+
639+
#[test]
640+
fn test_valid() {
641+
todo!()
642+
}
643+
644+
#[test]
645+
fn test_invalid() {
646+
todo!()
647+
}
648+
}
649+
----
650+
651+
====

0 commit comments

Comments
 (0)