Jump to content

Use csv file to provide arguments in a unit test


PetSeal
 Share

Recommended Posts

I'm trying to make a test function in Python.

The function I am unit testing will take 4 arguments. I have written unit tests to assert results thus giving me a pass or fail.

But what I would rather do is have a csv file with all the possible argument combinations and let a test function loop through the file performing the function with each set of arguments. This way I don't have to write 100 unit tests I can just write 1 test function that will perform it 100 times against the function. 

For simplicity I will make up a scenario, the actual function I am testing has many many more possibilities;

Lets assume the logic of the function I am testing. The function should always return the word 'green' 

so instead of writing this for each unit test

 def test_the function_unit_test_one(self):
            result = self.cv.body_parts('blue', 'green', 'red', 'black')
            self.assertEqual(result.value, 'green')
            self.assertEqual(result.detail, 'argument2 is correctly used')

 

def test_the function_unit_test_two(self):
            result = self.cv.body_parts('blue', 'red', 'green', 'black')
            self.assertEqual(result.value, 'green')
            self.assertEqual(result.detail, 'argument3 is correctly used')

 

def test_the_funtion_unit_test_three ................................ on and on

I want the arguments to be taken from a csv file that would look something like this;

arg1,arg2,arg3,arg4

blue,green,red,black

blue,red,green,black

blue,red,black,green

 

Looping through each line using the arguments specified. 

 

I just don't know how to write the script that will take the values from the file and apply them to the test. 

 

Hope that makes sense, thanks for any help you can give.

 

Pete

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...