0

I am looking for an elegant way to run unit tests on multiple data, in this case a directory containing text files .

Consider a folder text containing several txt files, I want run this example unittest on each file ( for example by feeding a list of files from glob to my unittest )

Here is how I test a single hardcoded file .

import unittest
import parser

class TestParser(unittest.TestCase):
    text_path = 'text/journal.txt'

    def test_get_chapters(self):
        x = parser.get_chapters(self.text_path)
        self.assertGreater(len(x),1))

if __name__ = "__main__":
    unittest.main()

Thanks !!

Finger twist
  • 3,546
  • 9
  • 42
  • 52
  • 1
    Does this answer your question? [How do you generate dynamic (parameterized) unit tests in python?](https://stackoverflow.com/questions/32899/how-do-you-generate-dynamic-parameterized-unit-tests-in-python) Some of the answers there are a bit outdated, but the ddt suggestion is a solid one in my opinion. – manveti Jan 10 '20 at 02:34
  • I actually had a play with ddt when I was looking for a way, but I wasn't able to feed the data decorator a dynamic list of files :( – Finger twist Jan 10 '20 at 02:37
  • Yeah, the decorator is executed when the class is declared, so you have to have the list of files available at that point. If that doesn't work for you, you may have to go with one of the more roll-your-own suggestions. – manveti Jan 10 '20 at 02:41
  • Someone has a metaclass suggestion in the link you provided, trying this now .. – Finger twist Jan 10 '20 at 02:44
  • metaclasses seem to work ! – Finger twist Jan 10 '20 at 03:14

0 Answers0