Refactoring and optimize unittest