Add unit_test github action

This commit is contained in:
femisan 2025-03-02 15:15:17 +09:00
parent bc531652e9
commit 5988a3ffdd
3 changed files with 90 additions and 0 deletions

41
.github/workflows/unit_test.yml vendored Normal file
View file

@ -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

0
tests/__init__.py Normal file
View file

49
tests/test_tts.py Normal file
View file

@ -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)