The longtime frustration of not being able to clear the messages in tests is marked as a delivered idea in Summer '18 here Add support for clearing PageMessages via Apex.
A failing of that forum is that there never seems to be a link to what was actually done, leaving you back in Google land trying to figure out what that was. In this case I haven't found any new methods in the documentation, and can confirm that the second assert here fails using API 45:
System.assert(ApexPages.getMessages().size() > 0); ApexPages.getMessages().clear(); System.assert(ApexPages.getMessages().size() == 0);
So how are the messages cleared?
It was easy enough to find the
Release Notefor this feature by looking at
This method is also included in the documentation on the