You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为依赖用户输入的pdftospeech函数编写单元测试?

为pdftospeech函数编写单元测试的方法

你的函数依赖input()交互和全局engine对象,单元测试需要模拟这些外部依赖,避免实际输入和语音播放。下面用Python标准库unittest.mock实现测试:

测试前准备

  1. 确保测试文件与目标函数在同一目录,或已导入目标函数所在模块
  2. 导入依赖模块:unittest、unittest.mock中的patch和MagicMock

具体测试用例

1. 验证输入"male"时的逻辑正确性

测试输入"male"(支持大小写、前后空格)时,函数是否正确设置男性语音、语速,并调用朗读方法:

import unittest
from unittest.mock import patch, MagicMock
from your_module import pdftospeech  # 替换为你的函数所在模块名

class TestPdfToSpeech(unittest.TestCase):
    def test_male_voice_selection(self):
        # 模拟engine及返回的语音列表
        mock_engine = MagicMock()
        mock_voices = [MagicMock(id="male_voice_id"), MagicMock(id="female_voice_id")]
        mock_engine.getProperty.return_value = mock_voices
        
        # 模拟用户输入"male"
        with patch('builtins.input', return_value='  Male  '):
            # 替换全局engine为模拟对象
            with patch('your_module.engine', mock_engine):
                pdftospeech("测试文本")
                
                # 验证语音设置是否正确
                mock_engine.setProperty.assert_any_call("voice", "male_voice_id")
                # 验证语速设置为150
                mock_engine.setProperty.assert_any_call("rate", 150)
                # 验证朗读方法被正确调用
                mock_engine.say.assert_called_once_with("测试文本")
                mock_engine.runAndWait.assert_called_once()

2. 验证输入"female"时的逻辑正确性

类似男性语音测试,验证女性语音选择逻辑:

def test_female_voice_selection(self):
        mock_engine = MagicMock()
        mock_voices = [MagicMock(id="male_voice_id"), MagicMock(id="female_voice_id")]
        mock_engine.getProperty.return_value = mock_voices
        
        with patch('builtins.input', return_value='female'):
            with patch('your_module.engine', mock_engine):
                pdftospeech("测试文本")
                
                mock_engine.setProperty.assert_any_call("voice", "female_voice_id")
                mock_engine.setProperty.assert_any_call("rate", 150)
                mock_engine.say.assert_called_once_with("测试文本")
                mock_engine.runAndWait.assert_called_once()

3. 验证无效输入抛出指定异常

测试输入非male/female内容时,是否抛出正确的ValueError,且不执行朗读逻辑:

def test_invalid_voice_raises_error(self):
        mock_engine = MagicMock()
        
        with patch('builtins.input', return_value='other'):
            with patch('your_module.engine', mock_engine):
                with self.assertRaises(ValueError) as context:
                    pdftospeech("测试文本")
                
                # 验证错误信息匹配
                self.assertEqual(str(context.exception), "Choose from male or female")
                # 验证朗读方法未被调用
                mock_engine.say.assert_not_called()
                mock_engine.runAndWait.assert_not_called()

运行测试

将测试代码保存为test_pdftospeech.py,终端执行:

python -m unittest test_pdftospeech.py -v

核心要点

  • 用patch('builtins.input')模拟用户输入,避免测试时手动交互
  • 用MagicMock创建模拟engine对象,跟踪方法调用以验证逻辑
  • 替换全局engine为模拟对象,避免触发实际语音引擎功能

内容的提问来源于stack exchange,提问作者Rylie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 04:55:21