Skip to content

Learning material for the workshop 'Testing in Rust'

Notifications You must be signed in to change notification settings

corrode/testing-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Workshop Testing in Rust

Course Banner

This is a workshop about testing in Rust. It is intended for people who are already a bit familiar with Rust and want to learn more about testing in Rust.

Prerequisites

  • Basic knowledge of Rust.
  • A working Rust installation. (See https://rustup.rs/)
  • A working Rust IDE (e.g. Visual Studio Code with the Rust extension) or an editor.

Slides

Open the slides to follow along with the workshop.

Exercises

Go to the src/exercises module and follow the instructions there. You can start with basic.rs and then move on to more advanced exercises.

Workshop Topics

This workshop is about testing in Rust. We will cover the following topics:

  • Writing unit tests in the same file as the code
  • Returning Result from tests
  • Keep tests close to the code
  • Writing integration tests
  • Integration tests vs functional tests
  • Doctests
  • Test doubles
    • Mocks
    • Traits
  • Tips and tricks
    • You can return Result from tests
    • #[ignore] attribute
    • Testing panics #[should_panic]
    • #[cfg(test)] for convenience methods
  • Test isolation
    • Isolating the filesystem: tempdir
    • Isolating the network: HTTP mocking
    • Isolating external dependencies: mockall
  • Speeding up tests
    • Unit tests over integration tests
    • Faster integration tests: use a single compilation unit
    • cargo test runs tests in parallel
    • cargo nextest
    • sequential tests with --test-threads=1 or https://crates.io/crates/serial_test
  • Exploratory testing
    • Property-based testing
    • Mutation testing
  • Benchmark tests
  • Structuring code for better testing Onion Architecture / Hexagonal Architecture
  • Test coverage: cargo tarpaulin --out Html

Running the tests

If you want to run unit tests only, you can use the --lib flag:

cargo test --lib

To run only the integration tests, you can use the --test flag:

cargo test --test '*'

To run a specific test, you can use the --test flag with the test name:

cargo test --test fibonacci_is_prime

Notable Frameworks and Libraries

Test Runners

Parameterized Testing

  • rstest: Write parameterized tests in Rust.
  • test-case: Another parameterized testing library.

Property-Based Testing

  • quickcheck: Automated property-based testing with shrinking support.
  • proptest: Another property-based testing library.

Permutation Testing

  • loom: a testing tool for concurrent code, which runs a test many times, permuting the possible concurrent executions.

Mocking and Test Doubles

  • mockall: Trait-based mocking library.
  • mockito: HTTP mocking library.
  • httpmock: Another HTTP mocking library.

Testing Command Line Applications

Filesystem and Network Isolation

  • tempfile: Create temporary files and directories.

Testing Snapshots

  • insta: Snapshot testing library.

Faking Data

  • fakeit: Fake data generator library with 130+ functions.

Test Context Management

  • test-context: A library for writing tests with setup and teardown functions.

Little Helpers

  • pretty_assertions: Alternative to assert_eq! with better diffing and color support.
  • assertor: Fluent assertion library for Rust with readable messages.

Procedural Macro Testing

About

Learning material for the workshop 'Testing in Rust'

Topics

Resources

Stars

Watchers

Forks