test: improve async test setup

- Add pytest-asyncio for handling async tests
- Update test_llm.py with proper async test decorators
- Add more assertions to LLM test
This commit is contained in:
zhukang 2025-01-14 20:56:32 +08:00
parent 7514326279
commit 0cfcd3c576
2 changed files with 19 additions and 2 deletions

View File

@ -17,6 +17,7 @@ requires-python = ">=3.8"
[project.optional-dependencies] [project.optional-dependencies]
dev = [ dev = [
"pytest>=7.4.0", "pytest>=7.4.0",
"pytest-asyncio>=0.21.0",
"black>=23.7.0", "black>=23.7.0",
"isort>=5.12.0", "isort>=5.12.0",
"mypy>=1.4.1", "mypy>=1.4.1",
@ -26,10 +27,15 @@ dev = [
requires = ["hatchling"] requires = ["hatchling"]
build-backend = "hatchling.build" build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["."]
[tool.pytest.ini_options] [tool.pytest.ini_options]
testpaths = ["tests"] testpaths = ["tests"]
python_files = ["test_*.py"] python_files = ["test_*.py"]
addopts = "-v" addopts = "-v"
asyncio_mode = "strict"
asyncio_default_fixture_loop_scope = "function"
[tool.black] [tool.black]
line-length = 100 line-length = 100

View File

@ -1,8 +1,10 @@
"""Test LLM API call.""" """Test LLM API call."""
import asyncio import asyncio
import pytest
from config.config_loader import load_config from config.config_loader import load_config
import httpx import httpx
@pytest.mark.asyncio
async def test_llm_call(): async def test_llm_call():
"""Test LLM API call with a simple prompt.""" """Test LLM API call with a simple prompt."""
config = load_config() config = load_config()
@ -19,8 +21,8 @@ async def test_llm_call():
"messages": [ "messages": [
{"role": "user", "content": "Say hello and introduce yourself in one sentence."} {"role": "user", "content": "Say hello and introduce yourself in one sentence."}
], ],
"temperature": llm_config["temperature"], "temperature": llm_config.get("temperature", 0.7),
"max_tokens": llm_config["max_tokens"] "max_tokens": llm_config.get("max_tokens", 2000)
} }
print("Making API call to Deepseek...") print("Making API call to Deepseek...")
@ -40,6 +42,15 @@ async def test_llm_call():
print("\nResponse:") print("\nResponse:")
print(result["choices"][0]["message"]["content"]) print(result["choices"][0]["message"]["content"])
assert response.status_code == 200
assert "choices" in result
assert len(result["choices"]) > 0
assert "message" in result["choices"][0]
assert "content" in result["choices"][0]["message"]
content = result["choices"][0]["message"]["content"]
assert isinstance(content, str)
assert len(content) > 0
except httpx.HTTPStatusError as e: except httpx.HTTPStatusError as e:
print(f"HTTP error occurred: {e.response.text}") print(f"HTTP error occurred: {e.response.text}")
except Exception as e: except Exception as e: