Say somewhere (deep, deep down) in the code I am using random numbers to do something¹ cool. Tests should in general be deterministic, while the behavior of the release version of this part of the code explicitly makes use of the non-determinism/randomness of the random number generator.
So to get deterministic (functional) tests, I would like to set a fixed seed value:
size_t seed = 42; std::mt19937 rng; rng.seed(seed); But I also want to make sure that nothing strange (i.e., an exception) happens when I use random input and a random seed:
std::mt19937 rng; rng.seed(std::random_device()()); Obviously, a finite number of test runs can not ascertain that the code is correct; however, a large number of test runs can at least give you some confidence.
How do I best handle this? I thought about adding something like:
size_t seed = std::random_device()(); #ifdef TESTING seed = 42; #endif rng.seed(seed); but then, I can not have one test file (using gtest) that uses a random seed for some tests and a constant seed for some other tests (can I?).
¹ In my case: I choose one element from n uniformly and independently at random to partition the input.
EDIT: I am asking about functional tests, not unit tests.