进行单元测试

main
xxxiix 5 months ago
parent da84c99f77
commit 43845279fe

@ -0,0 +1,27 @@
'''
对analysisEmotion中的函数进行测试python -m unittest
其中对于文件读取模型加载和图表绘制一类的函数没有进行测试
'''
import unittest
import barrage.analysisEmotion as ba
class TestEmotion(unittest.TestCase):
count = [9, 100, 300, 152, 789, 666, 552, 310, 218, 20]
pro = [0.119, 0.340, 0.281, 0.988, 0.284, 0.331, 0.210, 0.956, 0.100, 0.673]
probability = [0.648, 0.302, 0.996, 0.289, 0.462, 0.331, 0.248, 0.210, 0.008, 0.127]
result = [0.5951, 0.30237623762376237, 0.993624584717608, 0.2935686274509804, 0.46177468354430384, 0.331, 0.24793128390596744, 0.21239871382636655, 0.008420091324200914, 0.153]
def setUp(self):
print('start')
def test_emoChange(self):
for i in range(10):
compu = self.probability[i] + (self.pro[i] - self.probability[i])/(self.count[i]+1)
self.assertTrue(compu, self.result[i])
def tearDown(self):
print('end')
if __name__ == '__main__':
unittest.main()

@ -0,0 +1,71 @@
'''
对getBarrage中的函数进行测试python -m unittest
由于测试的是爬取b站数据为了防止被ban所以手动添加了一定的延时
'''
import unittest
import time
import random
import barrage.getBarrage as bg
class TestBarrage(unittest.TestCase):
headers = {
'Cookie': '',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36'
}
# 以下是用到的测试数据
searchWord = '2024巴黎奥运会'
content_list = [
'//www.bilibili.com/video/BV1NtYZe2EFH',
'//www.bilibili.com/video/BV1CSYoerEHy',
'//www.bilibili.com/video/BV1mE4m1R71o',
'//www.bilibili.com/video/BV17zpxeQE9A',
'//www.bilibili.com/video/BV1Di421h7Q4',
'//www.bilibili.com/video/BV1tn4y1f7sR',
'//www.bilibili.com/video/BV1e1p2eMEKX',
'//www.bilibili.com/video/BV1NgiVecEns',
'//www.bilibili.com/video/BV1RQHcekEfU',
'//www.bilibili.com/video/BV1JbexeUELF'
]
cid_list = [
'25751522644',
'25751522791',
'25751522812',
'25751523009',
'5000401',
'5038962',
'5248',
'579',
'6871235',
'304154'
]
def setUp(self):
print('start')
def test_GetAllSearchVideoUrl(self):
for i in range(10):
time.sleep(0.5+random.random())
page = i + 1
o = page * 30
url = f'https://search.bilibili.com/all?keyword={self.searchWord}&from_source=webtop_search&spm_id_from=333.934&search_source=5&page={page}&o={o}'
self.assertTrue(type(bg.GetAllSearchVideoUrl(url, self.headers)) == list )
def test_GetVideoCid(self):
for i in range(10):
time.sleep(0.5+random.random())
url = 'https:' + self.content_list[i]
self.assertIsNotNone(type(bg.GetVideoCid(url, self.headers)))
def test_GetVideoBarrage(self):
for i in range(10):
time.sleep(0.5+random.random())
url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=' + self.cid_list[i]
self.assertTrue(type(bg.GetVideoBarrage(url, self.headers)) == list)
def tearDown(self):
print('end')
if __name__ == '__main__':
unittest.main()

@ -0,0 +1,50 @@
'''
对handleData中的函数进行测试python -m unittest
其中对于文件读取一类的函数没有进行测试
'''
import unittest
import numpy as np
import pandas as pd
import barrage.handleData as bh
class TestData(unittest.TestCase):
origin_list = [
'123',
'666',
'999',
'ai',
'555',
'abc',
'你我他',
]
keyWord_list = [
['1'],
['6'],
['9'],
['ai'],
['5'],
['2'],
['3'],
[''],
['ab'],
['i']
]
def setUp(self):
print('start')
def test_ChangeDfToString(self):
for _ in range(10):
df=pd.DataFrame(np.random.randn(4,4),index=list('ABCD'),columns=list('ABCD'))
self.assertTrue(type(bh.ChangeDfToString(df)) == str)
def test_GetKeyFromList(self):
for i in range(10):
self.assertTrue(type(bh.GetKeyFromList(self.keyWord_list[i], self.origin_list)) == list)
def tearDown(self):
print('end')
if __name__ == '__main__':
unittest.main()

@ -0,0 +1,34 @@
'''
对wordCloud中的函数进行测试python -m unittest
对于其中文件读取和绘制图片一类的函数没有进行测试
'''
import unittest
import barrage.wordCloud as bw
class TestWordCloud(unittest.TestCase):
word_list = [
'盖 闻 天地 之 数 有 十二万 九千 六百 岁 为 一元',
'将 一元 分为 十二 会 乃 子 丑 寅 卯 辰 巳 午 未 申 酉 戌 亥 之 十二支 也',
'每 会 该 一万 八百岁',
'天气 下降 地气 上升 天地 交合 群物 皆 生',
'那座 山 正当 顶上 有 一块 仙石',
'其 石 有 三丈 六尺 五寸 高 有 二丈 四尺 围圆 三丈 六尺 五寸 高 按 周天 三百六十五度 二丈 四尺 围圆 按 政历 二十四气',
'盖 自 开辟 以来 每 受 天真 地 秀 日精 月华 感 之 既久 遂有 灵通 之 意',
'内 育 仙胞 一日 迸裂 产 一 石 卵',
'因 见风 化作 一个 石猴 五官 俱备 四肢 皆全 便就 学 爬 学 走 拜了 四方',
'目 运 两道 金光 射 冲 斗府'
]
def setUp(self):
print('start')
def test_ChangeDfToString(self):
for i in range(10):
self.assertTrue(type(bw.ChangeToFreq(self.word_list[i])) == dict)
def tearDown(self):
print('end')
if __name__ == '__main__':
unittest.main()
Loading…
Cancel
Save