How do you get faster tests?

I'm just getting started and am looking to speed up the test suite, so it's really fun to work with. With the least code possible, changes are taking a full second or more to report back. Is it possible to speed this up?

Coming from scripted languages like Node.js, I'm used to changes reporting back in <10ms.

Any chance there's an option I'm missing on cargo or a package, or maybe even a RustScript of sorts that can be used for just cruising through the green-red-refactor cycle?

A really quick example

So far, I've got this code under test:

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

Then, I'll start the cargo watch (Not bad at ~20ms):

$ cargo watch -x test -x check
[Running 'cargo test && cargo check']
    Finished test [unoptimized + debuginfo] target(s) in 0.01s
     Running target\debug\deps\adder-8e5e898cf523b816.exe

running 1 test
test tests::it_works ... ok

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

   Doc-tests adder

running 0 tests

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

    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
[Finished running. Exit status: 0]

Then, I make a change and it takes about 1 full second:

[Running 'cargo test && cargo check']
   Compiling adder v0.1.0 (C:\dev\rusttest\adder)
    Finished test [unoptimized + debuginfo] target(s) in 0.84s
     Running target\debug\deps\adder-8e5e898cf523b816.exe

running 1 test
test tests::it_works ... FAILED

failures:

---- tests::it_works stdout ----
thread 'tests::it_works' panicked at 'assertion failed: `(left == right)`
  left: `3`,
 right: `4`', src\lib.rs:5:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.


failures:
    tests::it_works

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

error: test failed, to rerun pass '--lib'
[Finished running. Exit status: 0]

Well, cargo has to compile the whole crate + all tests whenever you run a test, so until Rust improves build times you're going to be out of luck.

1 second is a very fast build, btw. :slight_smile:

Rust has an inherently slow compiler. It's being worked on, but it has to do a lot more things than node to run an executable:

  • Parse (Both have this)
  • Interpret (Both have this) into ast
  • Lifetime checks
  • Type checks
  • LLVM optimizations & codegen
  • Linking

Specifically type checks and llvm optimizations/codegen can be slow.

I wouldn't expect it to reach ~10ms in the near (~6 months, perhaps a year) future. If you want really fast compilation times, then you should stick to standard library and your own code... and potentially being unproductive since the standard library in rust is small by design. Adding more dependencies increases compile times, but allows you to do more things.

You don't need to run both test and check. test compiles your project, so check is redundant.