0

I'm creating tests for a class using doctest. One of its functions has a random component so its output can vary. I have tried using random.seed() but it changes when I run the file again.

I have tried (too) to use doctest ellipsis but the number can change as the following example:

>>> my_function(params)
9.99
>>> my_function(params)
10.01

Is there a function to test "around" a number, for example using 10 in test and accepting 9.99 and 10.01 as well?

Aleharu
  • 170
  • 1
  • 9
  • 3
    Normally you'd do something like `abs(my_function(params) - my_function(params)) < 0.01`. Some other options at https://stackoverflow.com/q/2428618/3001761. – jonrsharpe Jan 25 '21 at 16:21
  • Havent tried doctest, but in the `unittest` module, you can use the `assertAlmostEqual` function. See https://www.geeksforgeeks.org/python-unittest-assertalmostequal-function/ – oskros Jan 25 '21 at 16:23
  • @RandomDavis How can I use it inside a test (in Python doctest)? I'm looking for an "ellipsis" kind of function – Aleharu Jan 25 '21 at 16:31

0 Answers0