Skip to content

Commit 660cde8

Browse files
docs(code-style-guide): Add unit test function name rule (#667)
* docs(code-style-guide): Add unit test function name rule * Update modules/contributor/pages/code-style-guide.adoc Co-authored-by: Nick <10092581+NickLarsenNZ@users.noreply.github.com> --------- Co-authored-by: Nick <10092581+NickLarsenNZ@users.noreply.github.com>
1 parent 413528d commit 660cde8

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

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

Lines changed: 61 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,63 @@ 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 a redundant `test` prefix or
604+
suffix.
605+
606+
It results in the output of `cargo test` containing superfluous mentions of
607+
"test", especially when the containing module is called `test`. For example:
608+
`my_crate::test::test_valid`.
609+
610+
Instead, use an appropriate name to describe what is being tested. The previous
611+
example could then become: `my_crate::test::parse_valid_api_version`.
612+
613+
[TIP.code-rule,caption=Examples of correct code for this rule]
614+
====
615+
616+
[source,rust]
617+
----
618+
#[cfg(test)]
619+
mod test {
620+
use super::*;
621+
622+
#[test]
623+
fn parse_valid_api_version() {
624+
todo!()
625+
}
626+
627+
#[test]
628+
fn parse_invalid_api_version() {
629+
todo!()
630+
}
631+
}
632+
----
633+
634+
====
635+
636+
[WARNING.code-rule,caption=Examples of incorrect code for this rule]
637+
====
638+
639+
[source,rust]
640+
----
641+
#[cfg(test)]
642+
mod test {
643+
use super::*;
644+
645+
#[test]
646+
fn test_valid() {
647+
todo!()
648+
}
649+
650+
#[test]
651+
fn test_invalid() {
652+
todo!()
653+
}
654+
}
655+
----
656+
657+
====

0 commit comments

Comments
 (0)