mercredi 4 janvier 2017

Using exec() for automated testing of functions

I want to create a testing Framework for some of my python functions (although this approach might work for any other language)

My idea is to create a file where I'll have:

  • The name of function to be tested
  • The input parameters for this function
  • The expected output

My file will look like:

# Test 1: Description of the test    # foo test1
Function: foo
Input: param1, param2, param3
Output: out1, out2

# Test 2: Description of the test    # foo test2
Function: foo
Input: param1, param2, param3
Output: out1, out2

# Test 3: Description of the test    # bar test1
Function: bar
Input: param1, param2, param3, param4
Output: out1, out2, out3

I can then read this file and start executing the tests.

My initial idea was to first build the command in a string:

command = function + '(' + ''.join(inputs) + ')'

and then using exec() to execute each of the commands

exec(command)

However I'm guessing if this is a good idea or there are better ideas to do that.

I say that because exec and eval are considered bad practices to be avoided when possible.

So my question is: Is my approach OK or there is an easy other way of doing this that I'm missing?

Aucun commentaire:

Enregistrer un commentaire