root-11

minimal_test_discovery

Apr 21st, 2020
327
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. def adder(a,b):  # function to test.
  4.     """ adds a to b"""
  5.     return a+b
  6.  
  7. options = [9, 9.0, [9], "9"]  # options for test function
  8.  
  9.  
  10. test_template = """
  11.  
  12. def test_adder_{}():
  13.    _ = adder{}
  14. """   # -- template for test that fail --
  15.  
  16.  
  17. def test_discovery():
  18.     from pathlib import Path
  19.     from itertools import product
  20.  
  21.     test_number = 0
  22.     for c in product(*[options, options]):
  23.         test_number += 1
  24.         try:
  25.             output = adder(*c)
  26.         except Exception:
  27.             new_test = test_template.format(test_number, c)  # write up test.
  28.             with Path(__file__).open('a') as fo:  # append it to the file.
  29.                 fo.write(new_test)
  30.  
  31.  
  32. test_discovery()  # main callable used to populate tests.
  33.  
  34. # test that fail will be written from here.
RAW Paste Data