Factor out useful code from testsuite