-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_runme_template.py
executable file
·87 lines (69 loc) · 2.09 KB
/
test_runme_template.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
"""
Unit Tests for the --Problem Name-- problem
for Google Code Jam --Year--
--Round--
Link to problem description:
--Link--
Author:
Chris Nitsas
(nitsas)
Language:
Python 3(.4)
Date:
--Date--
Usage:
python3 test_runme.py
"""
import io
import os
import sys
import unittest
# modules I've written:
import runme
def are_extra_samples_present():
return os.path.isfile('extra_sample.in') and os.path.isfile('extra_sample.out')
def contents_of(output_file):
with open(output_file, 'r', encoding='utf-8') as f:
return f.read()
def output_of_runme_on(input_file):
# call runme.main and get its output into from_main
with io.StringIO() as target_output_stream:
# redirect stdout to an io.StringIO object to run main
sys.stdout, old_stdout = target_output_stream, sys.stdout
runme.main(input_file)
from_main = target_output_stream.getvalue()
# get original stdout back
sys.stdout = old_stdout
return from_main
class TestRunme(unittest.TestCase):
"""
Simple tests for the --Problem Name-- problem
for Google Code Jam --Year--
--Round--
"""
# define if needed
# def setUp(self):
# pass
#
# define if needed
# def tearDown(self):
# pass
#
# def test_something(self):
# # use self.assertEqual(), self.assertTrue() or self.assertRaises()
# pass
#
def test_main_on_sample_in(self):
input_file, output_file = 'sample.in', 'sample.out'
# compare runme.main's results with sample.out's contents
self.assertEqual(output_of_runme_on(input_file),
contents_of(output_file))
@unittest.skipIf(not are_extra_samples_present(), 'no extra samples')
def test_main_on_extra_sample_in(self):
input_file, output_file = 'extra_sample.in', 'extra_sample.out'
# compare runme.main's results with extra_sample.out's contents
self.assertEqual(output_of_runme_on(input_file),
contents_of(output_file))
if __name__ == '__main__':
unittest.main()