Skip to content

Creating TestCase

Minho Ryang edited this page Mar 15, 2016 · 10 revisions

terminal_capsule.py

terminal_capsule.py는 2가지 버전이 있는데, 하나는 라이브러리용, 다른 하나는 실행용입니다.

(OnlineJudgeServer/terminal_capsule.py라이브러리용입니다.)

자세한 실행 예시가 아래에 나와 있지만, [TL;DR].

  1. Input 리다이렉션을 사용할 수 없습니다!
  2. Testcase를 생성할 폴더로 간다. 같은 pyenv 환경을 사용하고 있어야 합니다.
  3. python ~/online-judge/terminal_capsule.py 테스트타겟.py -j 00.json
    • 테스트타겟.py를 실행시키고, 거기서 사용자가 입력하고 그를 통해 받은 출력을 00.json으로 저장합니다.
  4. python ~/online-judge/terminal_capsule.py 테스트타겟.py -j 00.json -m playback
    • 위에서 생성한 00.json을 확인합니다.

terminal_capsule.py 설명 시작!

사용자용

python terminal_capsule.py --help

Usage: terminal_capsule.py [OPTIONS] PROGRAM

  Entrypoint for Terminal Capsule.

Options:
  -m, --mode [validate|playback|capture]
  -j, --json PATH                 Generate .json (or playback/validate)
  -o, --out FILENAME              Generate .out  (or logging)
  -t, --timeout INTEGER           Set timeout (default: .05s, validate: 2.5s)
  --help                          Show this message and exit.

개발자용

import terminal_capsule
help(terminal_capsule)

    NAME
        terminal_capsule - TerminalCapsule: CAPTURE, PLAYBACK, and VALIDATE I/O for ProcessCapsule.

    FUNCTIONS
        Capture(this_program, to_json=None, logfile=None, timeout=None)
            Run `this_program` by ProcessCapsule and Capture I/O `to_json`.

        Playback(this_program, from_json, logfile=None, timeout=None)
            Read I/O `from_json` and Playback it to `this_program`.

        Validate(this_program, from_json, logfile=None, max_retries=50, timeout=None)
            Read I/O `from_json` and Validate it to `this_program`.

Capture 모드

python terminal_capsule.py test5.py

테스트용 프로그램 (여기서는 test5.py)을 실행시키고, in/out 내용을 전달합니다.

Captured JSON Format

-j--json 옵션을 붙여, in/out 내용을 json으로 저장합니다.

python terminal_capsule.py test5.py -j test.json

[
  [
    null,
    "Width of Square = "
  ],
  [
    "12",
    "12\r\nThickness of Square = "
  ],
  [
    "2",
    "2\r\n"
    "************\r\n"
    "************\r\n"
    "**        **\r\n"
    "**        **\r\n"
    "**        **\r\n"
    "**        **\r\n"
    "**        **\r\n"
    "**        **\r\n"
    "**        **\r\n"
    "**        **\r\n"
    "************\r\n"
    "************\r\n"
    "\r\n"
  ]
]

(stdin, stdout+stderr)의 순서대로 누적되어 있습니다.

Captured .out

-o--out 옵션을 붙여, out 내용을 .out으로 저장합니다.

python terminal_capsule.py test5.py -o test.out < test.in

Playback(재생) 모드

python terminal_capsule.py test5.py -j test.json --mode playback

Width of Square = 12
Thickness of Square = 2
************
************
**        **
**        **
**        **
**        **
**        **
**        **
**        **
**        **
************
************

Validate(검사) 모드

python terminal_capsule.py test5.py -j test.json --mode validate

-----
Input 0 None
Expected 0 b'Width of Square = '
Output 0 b'Width of Square = '
[PASS] 0
-----
Input 1 b'12'
Expected 1 b'12\r\nThickness of Square = '
Output 1 b'12\r\nThickness of Square = '
[PASS] 1
-----
Input 2 b'2'
Expected 2 b'2\r\n************\r\n************\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n************\r\n************\r\n\r\n'
Output 2 b'2\r\n************\r\n************\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n**        **\r\n************\r\n************\r\n\r\n'
[PASS] 2

ETC