Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore a test based on environment variable

Tags:

rust

There are a few tests that's running locally but not on Github workflows. I've spent quite some time but not able to debug and fix them. For now, I want to ignore them on ci/cd but run them locally. Since, Github provides a handy environment variable CI, which always remains true, can I use that to ignore test?

I don't want to wrap the whole function code under if(env::var("CI").is_ok()). Is there any better way?

like image 819
Krishna Avatar asked May 14 '26 11:05

Krishna


1 Answers

One way you could go about doing this is by adding a feature to your Cargo.toml file, like ci or something similar. And then have your GitHub action compile with that feature enabled, and have a conditional compilation attribute on the tests in question.

To do this, you would first add a new feature to your Cargo.toml file:

[features]
ci = []

Then in your Rust code on a test you could write something like this:

#[test]
#[cfg_attr(feature = "ci", ignore)]
fn test_some_code() {
    println!("This is a test");
}

Now locally if you run cargo test you will see the test run, but if you run cargo test --features ci you will see the test shows ignored, like below:

enter image description here

Now you just have to change your GitHub action to compile with this feature. If your action looks something like this:

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Build
      run: cargo build --verbose
    - name: Run tests
      run: cargo test --verbose

Then add --features ci to the end of both the cargo build and cargo test commands.

For more information on conditional compilation, this is in the Rust book: https://doc.rust-lang.org/reference/conditional-compilation.html

For more information on features in Cargo, this is in the Cargo book: https://doc.rust-lang.org/cargo/reference/features.html

like image 104
pokeyOne Avatar answered May 19 '26 00:05

pokeyOne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!