This question already has an answer here:
I have two test files located in the tests
directory. Each test file contains a test function and a common struct. For example, they both contain:
#[macro_use] extern crate serde_derive;
#[derive(Deserialize)]
struct MyStructure {
a_value: u8,
}
#[test]
// ...some test functions...
The struct MyStructure
is exactly the same between the two test files. As my struct uses a macro from the serde_derive
crate, the line #[macro_use] extern crate serde_derive
is required.
In order to avoid code repetition, I would like to declare my struct only once. From what I understand, every file from the tests
directory is compiled as an individual crate, so it seems impossible to edit the test file and put the struct definition into a separate file in the tests
directory:
#[macro_use] extern crate serde_derive;
mod structure;
#[test]
// ...some test function...
#[macro_use] extern crate serde_derive;
#[derive(Deserialize)]
struct Structure {
a_value: u8,
}
This first try leads to error[E0468]: an extern crate loading macros must be at the crate root
. This is normal as structure
is now a sub-module.
Removing #[macro_use] extern crate serde_derive;
from structure
doesn't help either, as structure
is compiled as an individual crate, and now Deserialize
cannot be found: error: cannot find derive macro Deserialize in this scope
.
What is the proper way to move MyStructure
(including the macro usage) into a separate common file and define it only once for all my test files?
Aucun commentaire:
Enregistrer un commentaire