Skip to content

Instantly share code, notes, and snippets.

@root-11
Created April 21, 2020 10:31
Show Gist options
  • Save root-11/647889d3d4cb5873e2391cfcdbdb744f to your computer and use it in GitHub Desktop.
Save root-11/647889d3d4cb5873e2391cfcdbdb744f to your computer and use it in GitHub Desktop.
A minimal test generator example to automatic creation of tests when code fails.
def adder(a,b): # function to test.
""" adds a to b"""
return a+b
options = [9, 9.0, [9], "9"] # options for test function
test_template = """
def test_adder_{}():
_ = adder{}
""" # -- template for test that fail --
def test_discovery():
from pathlib import Path
from itertools import product
test_number = 0
for c in product(*[options, options]):
test_number += 1
try:
output = adder(*c)
except Exception:
new_test = test_template.format(test_number, c) # write up test.
with Path(__file__).open('a') as fo: # append it to the file.
fo.write(new_test)
test_discovery() # main callable used to populate tests.
# test that fail will be written from here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment