Search code examples
pythonpython-3.xteamcitypython-unittestrunner

Is it possible to implement multiple test runners in pyunitest? while only running the test suite once


if __name__ == '__main__':
    if is_running_under_teamcity():
        runner = TeamcityTestRunner()
    else:
        runner =  HTMLTestRunner.HTMLTestRunner(
                stream=outfile,
                title='Test Report',
                description='This is an example.'
                )
    unittest.main(testRunner=runner)

I am currently running some tests using unittest module in python this is my current code above. I am deploying this test setup on Teamcity, the first module allows me to convert the output into teamcity-messages and the second creates a html report of the results. Is there a way I can run both of these runners while only running one set of tests? The only option I can see at the minuete is to either try and combine both these modules into a hybrid or using another testing module that Teamcity supports. However I would like to keep the dependancies as low as possible

Any ideas would be great :)


Solution

  • Any ideas would be great :)

    Looks like you'll have to handroll it, looking at the code TeamcityTestRunner is a pretty simple extension of the standard TextTestRunner, however HTMLTestRunner is a way more complex beast.

    Sadly this is one area of the stdlib which is really badly architected: one could expect the test runner to be concerned solely with discovering and running tests, however it's also tasked with part of the test reporting rather than have an entirely separate test reporter (this test reporting is furthermore a split responsability with the test result, which shouldn't be part of that one's job description either).

    Frankly if you don't have any further customisation I'd suggest just using pytest as your test runner instead of unittest with a custom runner:

    • it should be able to run unittest tests fine
    • IME it has better separation of concerns and pluggability so having multiple reporters / formatters should work out of the box
      • pytest-html certainly has no issue generating its reports without affecting the normal text output
      • according to the readme teamcity gets automatically enabled and used for pytest
      • so I'd assume generating html reports during your teamcity builds would work fine (to test)
    • and you can eventually migrate to using pytest tests (which are so much better it's not even funny)