don't stop believing

hecate python module 설치와 기본 사용 (on Mac) 본문

Python/hecate & tmux

hecate python module 설치와 기본 사용 (on Mac)

Tongchun 2018. 9. 16. 12:05

Terminal Application 테스팅 도구를 찾다가 괜찮은 Python Library가 있길래 써봤습니다.

아래 github에서 받을 수 있습니다.

https://github.com/DRMacIver/hecate


테스트 스크립트 sample도 있네요.

https://github.com/DRMacIver/hecate/blob/master/tests/test_hecate.py


기본 개념은 Terminal Applicatin을 실행시켜 명령을 쓰고 결과로 나온 text를 검색할 수도 있습니다.

실행시킨 process를 종료시킬 수도 있네요.


(hecate가 그리스 여신이었네요. https://www.youtube.com/watch?v=ylF7rqVsLEg)


먼저 hecate에서 사용하는 library 및 package를 설치해 줍니다.

$ brew install libevent
$ pip install pytest tox

hecate에서 terminal based-applicatin과 연결해 주는 것이 tmux 입니다.

git으로 받은 hecate source 안에 tmux 설치파일이 있지만 설지가 잘 되지 않았습니다. 그래서 최신 버전으로 설치해 줍니다.

$ git clone https://github.com/tmux/tmux.git
$ cd tmux
$ sh autogen.sh
$ ./configure && make

tmux의 자세한 정보는 아래 링크에서 확인하세요.

https://github.com/tmux/tmux


이제 hecate를 다운받아 설치하겠습니다.

$ git clone https://github.com/DRMacIver/hecate.git

hecate를 다운받았다면 setup.py를 실행시켜 python libriry에 추가합니다.

setup의 옵션은 install입니다.

$ python setup.py install


이제 준비는 다 됐습니다.

test_hecate.py라고 파일을 만들고 간단한 테스트 코드를 작성해 봅니다.

# coding=utf-8

import hecate.runner as r
from hecate.hecate import Runner, AbnormalExit
import tempfile
import pytest
import sys
import os
import signal

Runner.print_on_exit = True

def test_can_run_vim():
    # 테스트에 사용할 파일 경로를 만들어 줍니다.
    # 현재 위치에서 hello_world.txt 로 생성해 사용합니다.
    f = os.path.dirname(__file__) + "hello_world.txt"

    # vim을 싱행합니다. 실힝되는 가상 terminal의 width와 height로 지정할 수 있습니다.
    with Runner("/usr/bin/vim", f, width=100, height=10) as h:
        
        # 첫번째 라인으로 이동합니다.
        h.press("gg")

        # 이전에 테스트했던 글을 지워줍니다.
        h.write("dd")
        h.write("dd")

        # i를 눌러 입력모드로 변환합니다.
        h.press("i")

        # 첫줄에 Hello world, 두번째 줄에 Goodboy world를 입력합니다.
        h.write("Hello world")
        h.press("Enter")
        h.write("Goodbye world")

        # 입력모드에서 나갑니다.
        h.press("Escape")

        # 파일을 저장하고 vim을 종료합니다.
        h.write(":wq")
        h.press("Enter")
        # Second enter because if running with unset environment in tox it will
        # complain that it can't write viminfo and tell you to press enter to
        # continue.
        h.press("Enter")
        h.await_exit()

    # 파일을 읽고 작성한 글이 있는지 확인합니다.
    with open(f) as r:
        text = r.read()
        assert "Hello world" in text
        assert "Goodbye world" in text

if __name__== "__main__":
    test_can_run_vim()

hecate의 세부 기능을 확인하고 싶다면 sample code를 확인하세요.

https://github.com/DRMacIver/hecate/blob/master/tests/test_hecate.py


업무에 사용해 봐야겠습니다.


'Python > hecate & tmux' 카테고리의 다른 글

tmux 설치 (on CentOS, Ubuntu)  (0) 2018.10.15
hecate 실습 (geth) 2  (0) 2018.09.20
hecate 실습 (geth) 1  (0) 2018.09.17
Comments