Search code examples
pythontest-suite

How do I extract the individual tests in a test suite object and execute them one by one


I have a test suite object that is run like this

suite = unittest.defaultTestLoader.discover("./tests")
unittest.TextTestRunner().run(suite)

So there are a bunch of python files in the tests folder which contain a lot of methods which tests something.

I wanna run the tests one by one and identify each test somehow.

So I asked chat gpt , how to do this

It gave me this

for test_case in suite:
        for test in test_case:
            print(test.id())

but got this error

AttributeError: 'TestSuite' object has no attribute 'id'

The reason I want to go through the testsuite object is that I don't want to write the code to find each test file.

So if there is any way please help me


Solution

  • I think you need to drill down one more level

    for test_case in suite:
        for tests in test_case:
            for test in tests:
                print(test.id())