Dynamic state exchange between # [test] s

How to split the state between tests if its external storage is not required (for example, environment variables, file, etc.)?

Stainless has a setup macro with a name before_each, and I’m thinking about the same, say, shared_valuesbut whose variables will be available for all tests, and which will also be run once (at the beginning of the test suite).

+7
source share
1 answer

There is nothing special about the tests. These are “just” functions that run on multiple threads. Therefore, one solution does the same thing as in other code: create a global mutable singleton :

#[macro_use]
extern crate lazy_static;

lazy_static! {
    static ref DATABASE: String = {
        format!("{}{}", "This was", " expensive")
    };
}

#[test]
fn one() {
    println!("{}", *DATABASE);
}

#[test]
fn two() {
    println!("{}", *DATABASE);
}

"", . , Drop lazy_static .

+7

Source: https://habr.com/ru/post/1666269/


All Articles