Rust

Testing

Unit tests, integration tests, doc tests, assertions, should_panic, benchmarks, test organization

18 คำถามสัมภาษณ์·
Mid-Level
1

Which attribute marks a function as a unit test in Rust?

คำตอบ

The #[test] attribute tells the Rust compiler that the annotated function is a unit test. When running cargo test, all functions marked with this attribute are automatically discovered and executed. Tests are compiled only with the test profile and are not included in the production binary.

2

Which Cargo command runs all tests in a Rust project?

คำตอบ

The cargo test command compiles the project with tests enabled and runs all unit tests, integration tests, and documentation tests. It automatically discovers functions marked with #[test], tests in the tests/ directory, and code examples in documentation. Options allow filtering tests by name or module.

3

Which macro should be used to check that two values are equal in a test?

คำตอบ

The assert_eq! macro compares two values and fails the test if they are not equal. It automatically displays the compared values on failure thanks to the Debug trait, making debugging easier. Unlike assert!, it provides a more informative error message showing the expected value and the actual value.

4

Where should integration tests be placed in a Rust project?

5

Which attribute should be used to test that a function causes a panic?

+15 คำถามสัมภาษณ์

เชี่ยวชาญ Rust สำหรับการสัมภาษณ์ครั้งถัดไป

เข้าถึงคำถามทั้งหมด flashcards แบบทดสอบเทคนิค แบบฝึกหัด code review และตัวจำลองสัมภาษณ์

เริ่มใช้ฟรี