Читаем Rust by Example полностью

test tests::test_divide ... ok

test tests::test_specific_panic ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests tmp-test-should-panic

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

<p id="running_specific_tests"><strong><a l:href="#running_specific_tests">Running specific tests</a></strong></p>

To run specific tests one may specify the test name to cargo test command.

$ cargo test test_any_panic

running 1 test

test tests::test_any_panic ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out

Doc-tests tmp-test-should-panic

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

To run multiple tests one may specify part of a test name that matches all the tests that should be run.

$ cargo test panic

running 2 tests

test tests::test_any_panic ... ok

test tests::test_specific_panic ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out

Doc-tests tmp-test-should-panic

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

<p id="ignoring_tests"><strong><a l:href="#ignoring_tests">Ignoring tests</a></strong></p>

Tests can be marked with the #[ignore] attribute to exclude some tests. Or to run them with command cargo test -- --ignored

#![allow(unused)]

fn main() {

pub fn add(a: i32, b: i32) -> i32 {

a + b

}

#[cfg(test)]

mod tests {

use super::*;

#[test]

fn test_add() {

assert_eq!(add(2, 2), 4);

}

#[test]

fn test_add_hundred() {

assert_eq!(add(100, 2), 102);

assert_eq!(add(2, 100), 102);

}

#[test]

#[ignore]

fn ignored_test() {

assert_eq!(add(0, 0), 0);

}

}

}

$ cargo test

running 3 tests

test tests::ignored_test ... ignored

test tests::test_add ... ok

test tests::test_add_hundred ... ok

test result: ok. 2 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out

Doc-tests tmp-ignore

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

$ cargo test -- --ignored

running 1 test

test tests::ignored_test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests tmp-ignore

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

<p id="documentation_testing"><strong><a l:href="#documentation_testing">Documentation testing</a></strong></p>

The primary way of documenting a Rust project is through annotating the source code. Documentation comments are written in markdown and support code blocks in them. Rust takes care about correctness, so these code blocks are compiled and used as tests.

/// First line is a short summary describing function.

///

/// The next lines present detailed documentation. Code blocks start with

/// triple backquotes and have implicit `fn main()` inside

/// and `extern crate `. Assume we're testing `doccomments` crate:

///

/// ```

/// let result = doccomments::add(2, 3);

/// assert_eq!(result, 5);

/// ```

pub fn add(a: i32, b: i32) -> i32 {

a + b

}

/// Usually doc comments may include sections "Examples", "Panics" and "Failures".

///

Перейти на страницу:

Похожие книги