From 95c65b1e2ecbc006f472601a6715931a68524e2e Mon Sep 17 00:00:00 2001 From: as6325400 Date: Mon, 7 Oct 2024 01:09:14 +0800 Subject: [PATCH] update unittest --- .github/workflows/unittest.yml | 2 -- tests/test_img.py | 16 ++-------------- tests/test_qart.py | 11 ----------- tests/test_qrcode.py | 11 ----------- 4 files changed, 2 insertions(+), 38 deletions(-) diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml index 15ad1b6..69e0dfb 100644 --- a/.github/workflows/unittest.yml +++ b/.github/workflows/unittest.yml @@ -18,13 +18,11 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - # 安裝系統依賴,確保 OpenCV 可以正確運行 - name: Install OpenCV dependencies run: | sudo apt-get update sudo apt-get install -y libgl1-mesa-glx libglib2.0-0 - # 安裝 Python 依賴 - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/tests/test_img.py b/tests/test_img.py index 6dbd219..0891ee2 100644 --- a/tests/test_img.py +++ b/tests/test_img.py @@ -9,47 +9,37 @@ class TestImage(unittest.TestCase): @patch('matplotlib.pyplot.imsave') def test_imgsave(self, mock_plt_save): - # 资源目录中的文件路径 resource_dir = "tests" resource_file = "1.png" resource_path = os.path.join(resource_dir, resource_file) - # 确保资源文件存在 self.assertTrue(os.path.exists(resource_path), f"Resource file {resource_path} does not exist") - - # 创建临时目录 with tempfile.TemporaryDirectory() as tmpdirname: temp_file = os.path.join(tmpdirname, resource_file) - # 将资源文件复制到临时目录中 + shutil.copy(resource_path, temp_file) - # 使用 NamedTemporaryFile 创建临时保存路径 with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as temp_output_file: - # 创建 Image 对象并使用临时文件路径 + img = Image(temp_file) img.SetModuleNums(20) - # 保存到临时文件路径,文件会在测试结束后自动删除 img.save(temp_output_file.name, mode="Modulebase") img.save(temp_output_file.name, mode="RGB") img.save(temp_output_file.name, mode="Grayscale") img.save(mode="OTSU") - # 确保 save() 调用了4次 self.assertEqual(mock_plt_save.call_count, 4) with tempfile.NamedTemporaryFile(suffix=".jpg", delete=True) as temp_output_file: - # 创建 Image 对象并使用临时文件路径 img = Image(temp_file) img.SetModuleNums(20) - # 保存到临时文件路径,文件会在测试结束后自动删除 img.save(temp_output_file.name, mode="Modulebase") img.save(temp_output_file.name, mode="RGB") img.save(temp_output_file.name, mode="Grayscale") img.save(mode="OTSU") - # 确保 save() 调用了4次 self.assertEqual(mock_plt_save.call_count, 8) @patch('matplotlib.pyplot.show') @@ -64,8 +54,6 @@ def test_imgshow(self, mock_plt_show): img.show("Grayscale") img.show("OTSU") img.show("Modulebase") - - # 确保 plt.show() 被调用过 self.assertEqual(mock_plt_show.call_count, 4) diff --git a/tests/test_qart.py b/tests/test_qart.py index f92bfd4..4185c08 100644 --- a/tests/test_qart.py +++ b/tests/test_qart.py @@ -9,30 +9,19 @@ class TestQart(unittest.TestCase): @patch('matplotlib.pyplot.imsave') def test_Qartsave(self, mock_plt_save): - # 资源目录中的文件路径 resource_dir = "tests" resource_file = "1.png" resource_path = os.path.join(resource_dir, resource_file) - - # 确保资源文件存在 self.assertTrue(os.path.exists(resource_path), f"Resource file {resource_path} does not exist") - # 创建临时目录 with tempfile.TemporaryDirectory() as tmpdirname: temp_file = os.path.join(tmpdirname, resource_file) - # 将资源文件复制到临时目录中 shutil.copy(resource_path, temp_file) - # 使用 NamedTemporaryFile 创建临时保存路径 with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as temp_output_file: Qr = Qart("Accepted") Qr.generate(resource_path, 6, "L", mask = 0, mode = "Normal") - # # 创建 Image 对象并使用临时文件路径 - # img = Image(temp_file) - # img.SetModuleNums(20) - - # 保存到临时文件路径,文件会在测试结束后自动删除 Qr.save() self.assertEqual(mock_plt_save.call_count, 1) with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as temp_output_file: diff --git a/tests/test_qrcode.py b/tests/test_qrcode.py index b409166..0ad89cf 100644 --- a/tests/test_qrcode.py +++ b/tests/test_qrcode.py @@ -10,22 +10,17 @@ class TestQrcode(unittest.TestCase): @patch('matplotlib.pyplot.imsave') def test_qrcodesave(self, mock_plt_save): - # 资源目录中的文件路径 resource_dir = "tests" resource_file = "1.png" resource_path = os.path.join(resource_dir, resource_file) - # 确保资源文件存在 self.assertTrue(os.path.exists(resource_path), f"Resource file {resource_path} does not exist") - # 创建临时目录 with tempfile.TemporaryDirectory() as tmpdirname: temp_file = os.path.join(tmpdirname, resource_file) - # 将资源文件复制到临时目录中 shutil.copy(resource_path, temp_file) - # 使用 NamedTemporaryFile 创建临时保存路径 with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as temp_output_file: Qr = Qrcode("Accepted") Qr.generate(6, "L", mask = 0, mode = "Normal") @@ -46,12 +41,6 @@ def test_qrcodesave(self, mock_plt_save): Qr = Qrcode("Accepted") Qr.generate(6, "Q", mask = 7, mode = "Normal") num = Qr.InformationMask() - # Qr.showdata() - # # 创建 Image 对象并使用临时文件路径 - # img = Image(temp_file) - # img.SetModuleNums(20) - - # 保存到临时文件路径,文件会在测试结束后自动删除 Qr.save() self.assertEqual(mock_plt_save.call_count, 3) with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as temp_output_file: