Python Forum

Full Version: Python unittest - running multiple tests from CSV file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
class TestSuites(unittest.TestCase):

    def completeTest(self):
        pathCSV = r'pathToCSV\testCSV.csv'

        with open(pathCSV, 'rb') as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                for item in row:
                    try:
                        getattr(MyFile.myClass, item)()
                    except AttributeError:
                        print("Unknown attribute", item, "ignored")

    @staticmethod
    def myTests():
        suite = unittest.TestSuite()
        suite.addTest(TestSuites('completeTest'))
        return suite

if __name__== "__main__":
    runner.run(MyFile.TestSuites.myTests())
I have this piece of code wich reads a CSV file and runs a test(completeTest). The CSV file looks like this:

startApp,stopApp
startApp,createNewProject,stopApp

so each line in the CSV should be a test. Now everything that comes from the CSV lies under one test in my code. I want to run a test for each line in the CSV? How can I do this?
Thank you