test: 添加依赖检测的测试代码
Some checks failed
Python application test / build (pull_request) Failing after 12m39s
Some checks failed
Python application test / build (pull_request) Failing after 12m39s
This commit is contained in:
59
tests/test_requirements_detection.py
Normal file
59
tests/test_requirements_detection.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import unittest
|
||||||
|
from unittest.mock import patch, Mock
|
||||||
|
from detection.requirements_detection import fetch_html, parse_html, format_results
|
||||||
|
|
||||||
|
# Assuming the functions from your provided code are imported here
|
||||||
|
# from your_module import fetch_html, parse_html, format_results, ...
|
||||||
|
|
||||||
|
|
||||||
|
# 测试网页抓取和结果报告的测试类
|
||||||
|
class TestWebScrapingAndReporting(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_fetch_html_success(self):
|
||||||
|
"""测试fetch_html在请求成功时返回正确的HTML内容。"""
|
||||||
|
with patch("requests.get") as mocked_get:
|
||||||
|
mocked_get.return_value.status_code = 200
|
||||||
|
mocked_get.return_value.text = "success"
|
||||||
|
url = "http://example.com"
|
||||||
|
result = fetch_html(url)
|
||||||
|
self.assertEqual(result, "success")
|
||||||
|
|
||||||
|
def test_fetch_html_failure(self):
|
||||||
|
"""测试fetch_html在请求失败时返回None。"""
|
||||||
|
with patch("requests.get") as mocked_get:
|
||||||
|
mocked_get.return_code.status_code = 404
|
||||||
|
url = "http://example.com"
|
||||||
|
result = fetch_html(url)
|
||||||
|
self.assertIsNone(result)
|
||||||
|
|
||||||
|
def test_parse_html(self):
|
||||||
|
"""测试parse_html能准确地解析HTML并提取预期的数据。"""
|
||||||
|
html_content = """
|
||||||
|
<table id="sortable-table">
|
||||||
|
<tbody>
|
||||||
|
<tr><td></td><td><a href="#">Link1</a><span>Span1</span></td></tr>
|
||||||
|
<tr><td></td><td><a href="#">Link2</a><span>Span2</span></td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
"""
|
||||||
|
expected = [("Link1", ["Span1"]), ("Link2", ["Span2"])]
|
||||||
|
result = parse_html(html_content)
|
||||||
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
|
def test_format_results(self):
|
||||||
|
"""测试format_results能正确格式化解析后的数据。"""
|
||||||
|
results = [("Package1", ["1.0", "2.0"]), ("Package2", ["1.5", "2.5"])]
|
||||||
|
expected_output = (
|
||||||
|
"Package Name: Package1\nVersion Ranges: 1.0, 2.0\n"
|
||||||
|
+ "--------------------------------------------------\n"
|
||||||
|
+ "Package Name: Package2\nVersion Ranges: 1.5, 2.5\n"
|
||||||
|
+ "--------------------------------------------------\n"
|
||||||
|
)
|
||||||
|
formatted_result = format_results(results)
|
||||||
|
self.assertEqual(formatted_result, expected_output)
|
||||||
|
|
||||||
|
# Additional tests can be added here for other functions.
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user