refactoring tests: moving common part to a function