반응형

해당 post는 특별한 건 없고, 단순히 개인적으로 공부할 때 사용하는 코드 구조이다.

 

project directory는 아래와 같다.

 

proj

 - Test 

    L Tester.py

    L TestFactory.py

 L main.py

 

우선 test 코드는 Tester.py 를 상속한 개별 class를 통해 진행하며, 

어떠한 Test를 진행할 지는 main.py에서 변수를 지정해주는 식이다.

# TestFactory.py



from Test.Tester import Enum
from Test.Tester import ETest

from Test.Printer import *


class TestFactory():

    test_dic = {}

    @classmethod
    def Init_Tester(cls):
        """
           수행할 test 종류는 여기서 세팅
           :return:
           """
        cls.test_dic[ETest.PRINTER] = Printer().DoTest
        pass

    @classmethod
    def GetTester(cls, test_val:ETest):
        """
        Test class들의 DoTest 함수를 반환.
        :param test_val: 원하는 test 종류
        :return:
        """
        return cls.test_dic.get(test_val, (lambda: None))

    pass
# main.py



from Test.TestFactory import *

def DoTest():
    """
    원하는 test에 따라서 test_val 지정.
    :return:
    """
    TestFactory.Init_Tester()
    test = TestFactory.GetTester(ETest.PRINTER)
    test()
    pass


if __name__ == '__main__':
    DoTest()
    pass

#Tester.py


from enum import Enum

class Tester:

    def __init__(self, test_loop):
        self.is_test_continue = True
        self.test_loop = test_loop
        pass

    def DoTest(self):
        if callable(self.test_loop):
            while(self.is_test_continue):
                self.test_loop()
        else:
            print(f'test ret : {self.test_loop}')
        print('Test is finish')
        pass

    def FinTest(self):
        print('Test finish requested')
        self.is_test_continue = False
        pass

    pass


class ETest(Enum):
    NONE = 0,
    PRINTER = 1,
    pass

code를 보면 알겠지만 Tester class를 상속받은 객체들의 testloop를 호출하게 된다.

 

각 Test에 따라서 __init__에서 실제 TestLoop를 지정해주면 된다.

main에서 Tetser와 관련된 애들을 import 시키고 싶지 않았기 때문에 TestFactory를 통해서 함수를 호출하게 했다.

 -> 아직 특정 import를 은닉화 하는 방법을 못찾았기 때문에 main에서 TestFactory를 통해서 Test에 접근이 가능하다. 

추후 공부 겸 test code 작성 시 posting 할 예정이다.

반응형

'Python > Python Script' 카테고리의 다른 글

OperatorStep_1 - 각종 연산자 관련 코드.  (0) 2020.06.22
Printer.py - string format 연습  (0) 2020.06.22
문법 정리- sequence type  (0) 2020.05.28
Posted by Sweetmeats_boy

블로그 이미지
Sweetmeats_boy

태그목록

Yesterday
Today
Total

달력

 « |  » 2025.2
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함