consistently save/restore state when running tests

It's not obvious, but this is a refactoring. There should be no behavior changes.

Created by  Kartik K. Agaram  on January 23, 2023
JOLUCIT6HFXFKYQ6PNGSNBF4GBYDA6ATVOITT2SYGJH7HBURTDDAC
Change contents