samedi 28 mars 2020

How can I share test code between Rust crates in a workspace?

I have several crates in a workspace. One crate define a Trait, that others implement. I would like to write a few test functions that just take the Trait and ensure all the invariants are always true, and that sample code work with all instances of the Trait. So I'd like to define a test suite, and each other crate should say "I define my tests as being this test suite, with my own implementation of the Trait". Is that possible?

I suppose I could define a macro in my library that generates all the tests using the Trait instance, but that would mix production and test code in my library. Can another crate in my workspace reference a test module present in the tests folder of my main crate?

So basically I have:

workspace
|-- crate1
    |-- src
        |-- lib.rs
    |-- tests
        |-- harness.rs
|-- crate2
    |-- src
        |-- lib.rs
    |-- tests
        |-- test2.rs

And I would like test2.rs to be able to use harness.rs. Is that possible?

Aucun commentaire:

Enregistrer un commentaire