jeudi 5 octobre 2017

Data Driven Tests with variables numbers of inputs with Python unittest

I have a dataframe of values that I want to use for testing my program with a variable number of points: df.columns = Index([Point, Input1, Input2, Output1, Output2]), each row being a different point, with inputs variables and outputs variables. Currently, I extract a series for each point and pass the values to my functions to test:

import pandas as pd
path = r'./data.csv'
df = pd.read_csv(path)
checked_values = df.iloc[0]

import unittest
class TestModule(unittest.TestCase):
    def setUp(self):
        self.out1, self.out2 = myFunction(values['input1'], values['input2'])

    def test_values(self):
        self.assertEqual(self.out1, values['output1'])
        self.assertEqual(self.out2, values['output2'])

However, I don't want to define the inputs and outputs within the body of the script as I want to be able to test a variable number of points (depending of the dataframe). An ugly workaround I used is to wrap the call to the main function:

if __name__ == '__main__':
    import pandas as pd
    path = r'./data.csv'
    df = pd.read_csv(path)
    global values
    labels = ['input1', 'input2', 'output1', 'output2']
    for index, row in df.iterrows():
        values = {key : row[key] for key in labels}
        unittest.main()

This is not good for two reasons: first it does not sum up the tests when I launch them (it run actually as many times as rows in the dataframe). Furthermore, it works only when I launch it from Spyder: if I start it from the command line with python -m unittest discover, all the tests fails with the error message NameError: name checked_values is not defined, which I expected. However, I want to be able to use the Test Discovery approach, so this workaround is no good fix for me.

Other posts on StackOverflow recommand the use of DDT, but the decorator approach does not suit me, as the number of data may change according to the data I want to check, and that I want to be able to use a setUp() for each Series of my data.

Aucun commentaire:

Enregistrer un commentaire