Consolidate similar tests to reduce duplication (#12871)

This commit is contained in:
alisonshao
2025-11-15 14:29:44 +08:00
committed by GitHub
parent 34851471b2
commit 67e6f1438d
7 changed files with 219 additions and 300 deletions
@@ -1,14 +1,3 @@
"""
python3 -m unittest openai_server.features.test_json_mode.TestJSONModeOutlines.test_json_mode_response
python3 -m unittest openai_server.features.test_json_mode.TestJSONModeOutlines.test_json_mode_with_streaming
python3 -m unittest openai_server.features.test_json_mode.TestJSONModeXGrammar.test_json_mode_response
python3 -m unittest openai_server.features.test_json_mode.TestJSONModeXGrammar.test_json_mode_with_streaming
python3 -m unittest openai_server.features.test_json_mode.TestJSONModeLLGuidance.test_json_mode_response
python3 -m unittest openai_server.features.test_json_mode.TestJSONModeLLGuidance.test_json_mode_with_streaming
"""
import json
import unittest
@@ -19,38 +8,13 @@ from sglang.test.test_utils import (
DEFAULT_SMALL_MODEL_NAME_FOR_TEST,
DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
DEFAULT_URL_FOR_TEST,
CustomTestCase,
popen_launch_server,
)
def setup_class(cls, backend):
cls.model = DEFAULT_SMALL_MODEL_NAME_FOR_TEST
cls.base_url = DEFAULT_URL_FOR_TEST
other_args = [
"--max-running-requests",
"10",
"--grammar-backend",
backend,
]
cls.process = popen_launch_server(
cls.model,
cls.base_url,
timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
other_args=other_args,
)
cls.client = openai.Client(api_key="EMPTY", base_url=f"{cls.base_url}/v1")
class TestJSONModeOutlines(unittest.TestCase):
@classmethod
def setUpClass(cls):
setup_class(cls, "outlines")
@classmethod
def tearDownClass(cls):
kill_process_tree(cls.process.pid)
class TestJSONModeMixin:
"""Mixin class containing JSON mode test methods"""
def test_json_mode_response(self):
"""Test that response_format json_object (also known as "json mode") produces valid JSON, even without a system prompt that mentions JSON."""
@@ -121,16 +85,46 @@ class TestJSONModeOutlines(unittest.TestCase):
self.assertIsInstance(js_obj, dict)
class TestJSONModeXGrammar(TestJSONModeOutlines):
class ServerWithGrammarBackend(CustomTestCase):
"""Base class for tests requiring a grammar backend server"""
backend = "xgrammar"
@classmethod
def setUpClass(cls):
setup_class(cls, backend="xgrammar")
cls.model = DEFAULT_SMALL_MODEL_NAME_FOR_TEST
cls.base_url = DEFAULT_URL_FOR_TEST
other_args = [
"--max-running-requests",
"10",
"--grammar-backend",
cls.backend,
]
cls.process = popen_launch_server(
cls.model,
cls.base_url,
timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
other_args=other_args,
)
cls.client = openai.Client(api_key="EMPTY", base_url=f"{cls.base_url}/v1")
class TestJSONModeLLGuidance(TestJSONModeOutlines):
@classmethod
def setUpClass(cls):
setup_class(cls, backend="llguidance")
def tearDownClass(cls):
kill_process_tree(cls.process.pid)
class TestJSONModeXGrammar(ServerWithGrammarBackend, TestJSONModeMixin):
backend = "xgrammar"
class TestJSONModeOutlines(ServerWithGrammarBackend, TestJSONModeMixin):
backend = "outlines"
class TestJSONModeLLGuidance(ServerWithGrammarBackend, TestJSONModeMixin):
backend = "llguidance"
if __name__ == "__main__":