Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

21
  • 2
    For non-deterministic algorithms you can save seed of RNG or mock it using either using fixed sequence or low discrepancy determinitistic series e.g. Halton sequence Commented Oct 8, 2018 at 5:49
  • 14
    @PaintingInAir If it's impossible to verify the algorithm's output, can the algorithm even be incorrect? Commented Oct 8, 2018 at 9:04
  • 5
    Unless your code involves some random element The trick here is to make your random number generator an injected dependency, so you can then replace it for a number generator which gives the exact result you want it to. This enables you to accurately test again - counting the generated numbers as input parameters as well. not deterministic (i.e. it won't produce the same output given the same input) Since a unit test should start from a controlled situation, it can only be non-deterministic if it has a random element - which you can then inject. I can't think of other possibilities here. Commented Oct 8, 2018 at 9:06
  • 3
    @PaintingInAir: Either or. My comment applies to both fast execution or fast test writing. If it takes you three days to calculate a single example by hand (let's assume you use the fastest method available that is not using the code) - then three days is what it shall take. If you instead based your expected test outcome on the actual code itself, then the test is compromising itself. That's like doing if(x == x), it's a pointless comparison. You need your two outcomes (actual: comes from the code ; expected: comes from your external knowledge) to be independent of one another. Commented Oct 8, 2018 at 9:20
  • 2
    It is still unit testable even if not deterministic, providing it complies with specifications and that compliance can be measured (e.g. distribution and spread for random) It may just require a great many samples to eliminate the risk of anomaly. Commented Oct 9, 2018 at 2:46