Remarks
본 글은 파이썬 - 기본을 갈고 닦자! / unittest - 단위테스트의 내용을 정리한 글입니다.
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
### test.py
import unittest
import os
def custom_function(file_name):
with open(file_name, 'rt') as f:
return sum(1 for _ in f)
class CustomTests(unittest.TestCase):
def setUp(self):
self.file_name = 'test_file.txt'
with open(self.file_name, 'wt') as f:
f.write("Good!")
def tearDown(self):
try:
os.remove(self.file_name)
except:
pass
### test_ 로 시작하는 method는 모두 test method가 됨
def test_line_count(self):
self.assertEqual(custom_function(self.file_name), 1)
def test_no_file(self):
with self.assertRaises(IOError):
custom_function("abc.txt")
def test_runs(self):
custom_function(self.file_name)
if __name__ == '__main__':
unittest.main()
$ python test.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
PREVIOUSEtc