Skip to content
Advertisement

Python unit test with base and sub class

I currently have a few unit tests which share a common set of tests. Here’s an example:

JavaScript

The output of the above is:

JavaScript

Is there a way to rewrite the above so that the very first testCommon is not called?

EDIT: Instead of running 5 tests above, I want it to run only 4 tests, 2 from the SubTest1 and another 2 from SubTest2. It seems that Python unittest is running the original BaseTest on its own and I need a mechanism to prevent that from happening.

Advertisement

Answer

Use multiple inheritance, so your class with common tests doesn’t itself inherit from TestCase.

JavaScript
Advertisement