Unit tests and clean shutdown

I came across this while adding to tests for Chimera.

Summary: make sure ‘teardown’ actions are in an AfterMethod (or AfterClass) annotated method, not in the test method.

In the ‘happy path’ (all tests pass), the test finishes by calling

ChimeraViewFrame.closeViewer(true);

which does a force close of the Jalview viewer and Chimera - as wanted.

But if a test assertion fails before that is reached, then it is not executed, and instead we hit the teardown, in this case:

AfterClass

Desktop.instance.closeAll_actionPerformed(null);

which results in a call to

ChimeraViewFrame.closeViewer(false);

which puts up a user dialogue “Do you want to close Chimera” - just what we wanted to avoid while unit testing.

Solution:
Move closeViewer(true) into an AfterMethod teardown instead.
That ensures it runs even if the test fails.
The TestNG equivalent of a finally block OutlookEmoji-😊.png.

Probably stating the obvious, but something to look out for when facing those annoying dialogues in tests.

mungo

The University of Dundee is a registered Scottish Charity, No: SC015096

···

Mungo Carstairs
Jalview Computational Scientist
The Barton Group
Division of Computational Biology
School of Life Sciences
University of Dundee, Dundee, Scotland, UK.
www.jalview.org
www.compbio.dundee.ac.uk