Segment statistics fail in Test with no window mode

I have a case where I have several segmentations and I need to test each of them in no window mode (test mode)

The normal way to do this is to disable all of them and the enable just the one I am testing, this is done with

        displayNode = segmentationNode.GetDisplayNode()
        if displayNode is not None:
            displayNode.SetVisibility(1)

However without the qSlicerMainWindow the above code never gets executed as there are no display nodes in the first place. So the question is how to enable/disable the segmentation node without using the display nodes?

The problem is that when the segmentation is called by SegmenationStatistic module it is never enabled and it treats it as it were no segmentation at all.

This is related with the failing tests here

You can add display nodes to the scene, regardless of having a main window or not. The problem should be somewhere else.

Add a breakpoint or trace print in slicer.util.lookupTopLevelWidget to get a stack trace. Then we’ll know what needs the main window, and see if it is really necessary.