Factorize code for tests.