Development/Language

클래스 내 함수 랜덤 호출하기

비완 2015. 4. 16. 18:29
반응형

요구사항

프로젝트에서 테스트자동화 업무를 담당하여 수행한적이 있었다.

개발담당자들이 개발한 API를 호출하여 테스트하는 업무였는데, 자동화시스템을 구현하여 랜덤으로 해당 API들을 호출하는 요구사항이 있었다.

 

해결

아래과 같이 해당 요구사항을 만족할 수 있어 코드를 정리해놓았다.

method_list = []  # list for contain test scripts


def __init__(self):
    list = inspect.getmembers(self, predicate=inspect.ismethod)

    for item in list:
        if item[0] not in ("__init__", "test", "runner"):  # 제외할 method들을 작성
            self.method_list.append(item[0])
            
    self.method_list.sort()


def randomTest(self, repeat=1):  # execute all test scripts in class randomly
    for tmp in range(repeat):
        func_name = random.choice(self.method_list)
        System.Debug(func_name)

        if hasattr(self, func_name):
            func = getattr(self, func_name)
            func()
반응형