Load self tests from a file external the main module

I have a Slicer ScriptedLoadableModule and in the same physical file a series of tests. I would like to place the tests in a separate file. However, I’m not able to get the SelfTests module in Slicer to load/recognize this tests file. Is this possible?

In the main module I have:

import Tests
class Test(Tests.Tests):
    """Test class for module."""

In the external file I have:

class Tests(ScriptedLoadableModuleTest):

Usually each SelfTest is a module (see for example TablesSelfTest). Modules register themselves as selftests. If you want to break away from this convention then you can register any function as selftest the same way as modules do it.

1 Like

I find that testing code mixed with implementation adds unnecessary noise for someone reading/maintaining it. This is, of course just my opinion.

So out of curiosity, if one took class TablesSelfTestTest and moved it into its own module, would the Slicer SelfTests module discover/load it?

Answered my own question: no. I was assuming ScriptedLoadableModuleTest inherited from ScriptedLoadableModule.

There are certainly advantages and disadvantages of keeping the implementation and test together. The default scripted module template uses one file to encourage developers who are new to Slicer (and potentially new to programming) to add automated testing while still allowing them to have their module in a single file. I agree that it adds noise to the implementation; and if the module grows larger then it is better to split it up to several files.


If you are interested in running Python tests without CTest/CMake then I would recommend to check out the SlicerPythonTestRunner extension. The SelfTests module is just for a slightly more convenient manual launching of some test (not that useful, maybe we should remove it to avoid confusion?).

Example SlicerPythonTestRunner test result:

SlicerPythonTestRunner is exactly what I want!! :crossed_fingers: