-
Notifications
You must be signed in to change notification settings - Fork 1
Creating TestCase
Minho Ryang edited this page Mar 15, 2016
·
10 revisions
terminal_capsule.py는 2가지 버전이 있는데, 하나는 라이브러리용, 다른 하나는 실행용입니다.
(OnlineJudgeServer/terminal_capsule.py
가 라이브러리용입니다.)
- Input 리다이렉션을 사용할 수 없습니다!
- Testcase를 생성할 폴더로 간다. 같은 pyenv 환경을 사용하고 있어야 합니다.
-
python ~/online-judge/terminal_capsule.py 테스트타겟.py -j 00.json
- 테스트타겟.py를 실행시키고, 거기서 사용자가 입력하고 그를 통해 받은 출력을 00.json으로 저장합니다.
-
python ~/online-judge/terminal_capsule.py 테스트타겟.py -j 00.json -m playback
- 위에서 생성한 00.json을 확인합니다.
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`.
python terminal_capsule.py test5.py
테스트용 프로그램 (여기서는 test5.py
)을 실행시키고, in/out 내용을 전달합니다.
-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)의 순서대로 누적되어 있습니다.
-o
나 --out
옵션을 붙여, out 내용을 .out으로 저장합니다.
python terminal_capsule.py test5.py -o test.out < test.in
python terminal_capsule.py test5.py -j test.json --mode playback
Width of Square = 12
Thickness of Square = 2
************
************
** **
** **
** **
** **
** **
** **
** **
** **
************
************
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
- 여기를 보면 테스트코드에 time.sleep()이 들어갔을 때도 잘 동작하는지를 볼 수 있습니다.
- 다른 문서에 아직 .in.txt와 .out.txt가 남아있는데 내가 Juice500에게 이야기를 덜 했거나 설득을 덜 하지 않았을까..
- 배터리 5% :)