Is there a sane way to unit test a stochastic process? For example say that you have coded a simulator for a specific system model. The simulator works randomly based on the seeds of the rngs so the state of the system cannot be predicted and if it can be every test should bring the system to a specific state before it attempts to test any method of a class. Is there a better way to do this?
In these cases, a stochastic testing is used where the original test data are determined by a multitude of random variables with respective distributions, and in order to compare the results obtained it is common to use the distributions of random variables.
Retrofit - probably the most popular networking client in Android development. Basically it allows to create HTTP client in an interface - you just add annotation with HTTP method, relative or absolute path and proper request is constructed.
The two obvious choices are to remove the randomness (that is, use a fixed, known seed for your unit tests and proceed from there), or to test statistically (that is, run the same test case a million times and verify that the mean and variance (etc.) match expectations). The latter is probably a better test of your system, but you'll have to live with some false alarms.
Here's a nice blog post that covers this topic. Basically you will need to inject a controlled randomness into the object under test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With