Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cargo tests #31

Closed
luhuimao opened this issue Oct 11, 2018 · 1 comment
Closed

Cargo tests #31

luhuimao opened this issue Oct 11, 2018 · 1 comment
Assignees

Comments

@luhuimao
Copy link
Contributor

The Rust community thinks about tests in terms of two main categories: unit tests and integration tests.

  • Unit tests are small and more focused, testing one module in isolation at a time, and can test private interfaces.
  • Integration tests are entirely external to your library and use your code in the same way any other external code would, using only the public interface and potentially exercising multiple modules per test.
@luhuimao luhuimao changed the title Cargo test Cargo tests Oct 11, 2018
@luhuimao
Copy link
Contributor Author

The bodies of test functions typically perform these three actions:

  • Set up any needed data or state.
  • Run the code you want to test.
  • Assert the results are what you expect.

a test in Rust is a function that’s annotated with the test attribute.

To change a function into a test function, add #[test] on the line before fn.

When you run your tests with the cargo test command, Rust builds a test runner binary that runs the functions annotated with the test attribute and reports on whether each test function passes or fails.

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

@lsf1001 lsf1001 closed this as completed Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants