Compile: this test must pass

I have a number of unit tests marked with

#[test]

Now, is there a way to mark some of these tests as: always run on recompile, and if this test fails, have the compilation process fail ?

No, there's not. Tests are not present during compilation unless you're, you know, testing. Why is this something that you want to do?

2 Likes

There are certain invariants where if they ever fail, everything should stop.

Then why not just run tests in CI?

I'm not using Continuous Integration.

Maybe I should start running a CI system. Any recommendations for what works well with Rust ?

If you can make the tests const fns, there's

1 Like

Well Fasterthanlime, as he describes in this article has found CircleCI very effective (and also paid). My needs are simpler and Github Actions is sufficient for me. But it is true, as he mentions in his article, the VM specs of Github Actions is rather meagre and it is very difficult to locally verify if the action specs work out correctly.

1 Like

This sounds like the perfect use case for CI.

Normally I'll set my GitHub repo up so the only way code can get into master is via a pull request, and PRs can only be merged if CI says all tests are passing.

3 Likes

I use GitHub Actions. My situation is a bit involved as I need to check a large number of things, but the time-rs/time repository is a go-to example for thorough checks.

That said, you could do something along the lines of what @scottmcm said. panic!() in const contexts has already landed on stable. So if your "test" can be const fn that returns a boolean, you could easily do

const _: () = if foo() {
    ()
} else {
    panic!("failure")
};

This will guarantee that your code executes at compile-time. A CI setup is definitely preferable, though.

This is probably going off topic, but are there any self hosted solutions (trying to avoid cloud services) via apt-get install or yum install ?

I feel like at core, all I need is something like:

loop {
  wait for git push or other event
  cargo test
  beep loudly if fail
}

I think the simplest solution here is to use a git commit (or push or receive) hook to run tests on each commit and abort if there are failures – at least assuming that the tests run in a reasonably short time.

1 Like

The simplest version is just watch, which you probably have installed already.

Or set up your editor to run tests on save.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.