如何 运行 Rust 中的特定单元测试?

How to run a specific unit test in Rust?

我在名为 school-info 的包中进行了单元测试,并且有一个名为 repeat_students_should_not_get_full_marks.

的测试函数

我可以 运行 模块中的所有测试 cargo test --package school_info

cargo test test-name 将匹配包含 test_name 的 运行 测试,尽管这没有帮助。

如何才能 运行 只 repeat_students_should_not_get_full_marks 测试而不 运行 进行所有测试?我在文档中找不到执行此操作的命令。

使用 cargo test test-name 筛选包含 test-name 的测试。有可能它可以 运行 多次测试。测试函数是否在某个mod中并不重要,它仍然可以执行多个测试。

您可以通过添加 -- --exact 作为参数来避免这种情况。

如果您的测试不在任何 mod 中,您可以像这样简单地执行:

cargo test test_fn_name -- --exact

否则您需要提供具有完整命名空间的测试:

cargo test test_mod_name::test_fn_name -- --exact

对于您的情况,解决方案是:

cargo test --package school_info repeat_students_should_not_get_full_marks -- --exact