簡體   English   中英

如何在 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM