Skip to content

Commit 24b1294

Browse files
committed
add test
1 parent f67559c commit 24b1294

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

test_mini_spider.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,21 @@ def test_error_url_(self):
5757

5858
class DownloadWorkerTest(unittest.TestCase):
5959

60+
fm = ''.join(['file:///', os.path.join(FILE_DIR, 'test_download_file')])
61+
to = os.path.join(FILE_DIR, 'test_download_file_to')
62+
6063
# 初始化工作
6164
def setUp(self):
6265
pass
6366

6467
# 退出清理工作
6568
def tearDown(self):
6669
pass
70+
'''
71+
if os.path.exists(self.to):
72+
os.remove(self.to)
73+
'''
74+
6775

6876
@httprettified
6977
def test_get_url_text(self):
@@ -92,11 +100,24 @@ def test_get_url_text_404(self):
92100
dw = mini.DownloadWorker('http://www.test.com', '.', 'reg', 0, 3, 1, 1)
93101
dw.get_url_text()
94102

95-
96103
def test_get_url_text_wrong_url(self):
97104
dw = mini.DownloadWorker('http://wrong.url.me', '.', 'reg', 0, 3, 1, 1)
98105
dw.get_url_text()
99106

107+
def test_download_file(self):
108+
dw = mini.DownloadWorker('http://no.url.me', '.', 'reg', 0, 3, 1, 1)
109+
to = dw.url_to_localfile(self.fm)
110+
def del_file(to):
111+
if os.path.exists(to):
112+
os.remove(to)
113+
del_file(to)
114+
dw.download_file(self.fm)
115+
self.assertTrue(os.path.exists(dw.url_to_localfile(self.fm)))
116+
dw.download_file(self.fm)
117+
self.assertTrue(os.path.exists(dw.url_to_localfile(self.fm)))
118+
del_file(to)
119+
120+
100121
class SpiderManagerTest(unittest.TestCase):
101122

102123
# 初始化工作
@@ -122,8 +143,5 @@ def test_spidermanage_ini(self):
122143
)
123144

124145

125-
126-
127-
128146
if __name__ == '__main__':
129147
unittest.main()

0 commit comments

Comments
 (0)