-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathtest_input.py
More file actions
59 lines (50 loc) · 1.11 KB
/
test_input.py
File metadata and controls
59 lines (50 loc) · 1.11 KB
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
import io
import sys
def stub_stdin(testcase_inst, inputs):
'''
输入内容
:param testcase_inst:
:param inputs:
:return:
'''
stdin = sys.stdin
def cleanup():
sys.stdin = stdin
testcase_inst.addCleanup(cleanup)
sys.stdin = io.StringIO(inputs)
def stub_stdout(testcase_inst):
'''
输出内容
:param testcase_inst:
:return:
'''
stderr = sys.stderr
stdout = sys.stdout
def cleanup():
sys.stderr = stderr
sys.stdout = stdout
testcase_inst.addCleanup(cleanup)
sys.stderr = io.StringIO()
sys.stdout = io.StringIO()
# 用法举例
# def test_fun():
# x = int(input())
# print(x + 5)
#
#
# class UnitTest():
# def test_fun(self):
# print('请输入数字')
# stub_stdin(self, '2\n4\n') # 依次输入2,4
#
# stub_stdout(self)
# test_fun()
# self.assertEqual(str(sys.stdout.getvalue()), '7\n')
#
# stub_stdout(self) # 重置输出
# test_fun()
# self.assertEqual(str(sys.stdout.getvalue()), '9\n')
#
#
# if __name__ == '__main__':
# unittest.main()