samedi 7 juillet 2018

Python automated testing

Okay going out on a limb here and I hope I can get my point across before someone dismisses this post. So here me out to the end.

I have a simple function which will remove the last four letters of a string

def banana_split(s):
  s = str(s)
  return s[:-4]

print banana_split("Banana Wax")
print banana_split("image.jpg")
print banana_split("Pork Pie")

The function works fine. It does what it says on the tin, and will cut the last four characters off. As as way of removing a filename extension it won't work with string like "image.jpeg" or "file.gnu.zp" or "myfile.qwerty". I made that last one up, by the way. So the code is functional, but works inappropriately for the reason given.

My approach to testing would be, and this is where I fall short, I'm sure would be to create a random string:

def random_string(leng):
  c = "abcdefghijklmnopqrstuvwxyz. 1234567890_"
  s = ""
  for i in range (0, leng):
    s += "".join(random.choice(c))
  return s

print banana_split(random_string(8))

And if that was done enough times I'd begin to see where the patterns are. In which case I could see the what changes needed to be made.

My question is: Is there any way in Python to automate testing so that you can see that there are rules to be added to the function so it is made more appropriate? Or is this a human vs computer testing a big can of worms?

Aucun commentaire:

Enregistrer un commentaire