''' 对getBarrage中的函数进行测试python -m unittest 由于测试的是爬取b站数据,为了防止被ban,所以手动添加了一定的延时 代码覆盖率测试coverage run -m unittest discover ''' import unittest import time import random from barrage import 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()