How can I avoid running some tests in parallel?

后端 未结 3 771
灰色年华
灰色年华 2021-01-14 17:49

I have a collection of tests. There are a few tests that need to access a shared resource (external library/API/hardware device). If any of these tests run in parallel, they

3条回答
  •  清酒与你
    2021-01-14 18:29

    You can always provide your own test harness. You can do that by adding a [[test]] entry to Cargo.toml:

    [[test]]
    name = "my_test"
    # If your test file is not `tests/my_test.rs`, add this key:
    #path = "path/to/my_test.rs" 
    harness = false
    

    In that case, cargo test will compile my_test.rs as a normal executable file. That means you have to provide a main function and add all the "run tests" logic yourself. Yes, this is some work, but at least you can decide everything about running tests yourself.


    You can also create two test files:

    tests/
      - sequential.rs
      - parallel.rs
    

    You then would need to run cargo test --test sequential -- --test-threads=1 and cargo test --test parallel. So it doesn't work with a single cargo test, but you don't need to write your own test harness logic.

提交回复
热议问题