diff --git a/.github/workflows/unit_test.yml b/.github/workflows/unit_test.yml new file mode 100644 index 000000000..a5103e7ec --- /dev/null +++ b/.github/workflows/unit_test.yml @@ -0,0 +1,41 @@ +name: Run Tests + +on: + pull_request: + branches: + - "*" + types: + - opened + - reopened + - synchronize + push: + branches: + - main + +jobs: + test: + name: Run Pytest on Ubuntu and macOS + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + python-version: ["3.8", "3.9", "3.10", "3.11"] + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + pip install pytest + + - name: Run tests with pytest + run: | + pytest --maxfail=5 --disable-warnings --tb=short diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_tts.py b/tests/test_tts.py new file mode 100644 index 000000000..6385dd61e --- /dev/null +++ b/tests/test_tts.py @@ -0,0 +1,49 @@ +import os +import pytest +import time +from core.providers.tts.base import TTSProviderBase +from pydub.generators import Sine + +class DummyTTSProvider(TTSProviderBase): + def __init__(self, config, delete_audio_file=False): + super().__init__(config, delete_audio_file) + + def generate_filename(self): + return "test_tts.mp3" + + async def text_to_speak(self, text, output_file): + """使用 pydub 生成一个简单的 1s MP3 语音文件""" + sample_rate = 16000 # 16kHz + duration = 1000 # 1秒 + sine_wave = Sine(440).to_audio_segment(duration=duration).set_frame_rate(sample_rate) + sine_wave.export(output_file, format="mp3") + +@pytest.fixture +def tts_provider(): + config = {"output_file": "test_output.mp3"} + return DummyTTSProvider(config) + +def test_tts_generation(tts_provider): + test_text = "你好,这是一段测试语音。" + tts_file = tts_provider.to_tts(test_text) + + assert tts_file is not None, "TTS 生成的文件路径为空" + assert os.path.exists(tts_file), f"TTS 语音文件 {tts_file} 未生成" + + # 清理测试文件 + time.sleep(1) + os.remove(tts_file) + +def test_mp3_to_opus_conversion(tts_provider): + test_text = "你好,这是一段测试语音。" + tts_file = tts_provider.to_tts(test_text) + + if os.path.exists(tts_file): + opus_datas, duration = tts_provider.wav_to_opus_data(tts_file) + assert opus_datas, "MP3 转 OPUS 失败,未生成 OPUS 数据" + assert duration > 0, "转换后的 OPUS 时长应大于 0" + + # 清理测试文件 + time.sleep(1) + if os.path.exists(tts_file): + os.remove(tts_file)