Skip to content

Commit

Permalink
update unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
as6325400 committed Oct 6, 2024
1 parent d1c7b40 commit 95c65b1
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 38 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 2 additions & 14 deletions tests/test_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)


Expand Down
11 changes: 0 additions & 11 deletions tests/test_qart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 0 additions & 11 deletions tests/test_qrcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down

0 comments on commit 95c65b1

Please sign in to comment.