To add to pokeyOne's answer, you can include a build script that checks for the CI environment variable and enables a ci feature when it's present. To do this, you would add this build.rs to your project root:
fn main() {
if std::env::var("CI").is_ok() {
println!("cargo:rustc-cfg=feature=\"ci\"");
}
}
Note that build scripts are only run on clean builds, so to test this locally you'll need to run cargo clean between runs (this won't be an issue on CI since you're doing a clean build there).
$ cargo test # run all tests
$ cargo clean # make sure build.rs is re-run...
$ CI=1 cargo test # skip CI tests
Features are a special type of --cfg flag, used for conditional compilation. Instead of using a feature, you could instead use a single identifier --cfg flag, like so:
// build.rs
fn main() {
if std::env::var("CI").is_ok() {
println!("cargo:rustc-cfg=ci");
}
}
// src/main.rs
#[cfg(test)]
mod tests {
// ..(snip)..
#[test]
#[cfg(not(ci))]
fn breaks_on_ci() { ... }
}
This build.rs is equivalent to running
$ RUSTFLAGS="--cfg ci" cargo test
You can read more about build scripts, --cfg flags, and conditional compilation in the Rust book here.