Skip to content

Commit

Permalink
更新SDK单元测试逻辑 (baidubce#615)
Browse files Browse the repository at this point in the history
* 更新SDK单元测试逻辑

* 更新单测脚本

* 删除串行运行装饰器

---------

Co-authored-by: yinjiaqi <[email protected]>
  • Loading branch information
C9luster and yinjiaqi authored Nov 26, 2024
1 parent a512b59 commit eaabe80
Show file tree
Hide file tree
Showing 50 changed files with 7 additions and 133 deletions.
85 changes: 6 additions & 79 deletions python/tests/parallel_ut_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
# 可以CPU并行的单测用例
CPU_PARALLEL_RUN_UNITTEST = []

# CPU上仅能串行执行的单测用例
CPU_SERIAL_RUN_UNITTEST = []

# 分类未知,故在CPU上串行执行的单测用例
UNKNOWN_UNITTEST = []

Expand All @@ -69,7 +66,6 @@ def choose_test_case(file):
"""
skip_case_str = '@unittest.skip('
cpu_parallel_str = '@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL"'
cpu_serial_str = '@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL"'

with open(file, 'r') as f:
all_line = f.readlines()
Expand All @@ -82,10 +78,6 @@ def choose_test_case(file):
CPU_PARALLEL_RUN_UNITTEST.append(file.split("/")[-1])
return

if list(set([line.strip().find(cpu_serial_str) for line in all_line])) != [-1]:
CPU_SERIAL_RUN_UNITTEST.append(file.split("/")[-1])
return

UNKNOWN_UNITTEST.append(file.split("/")[-1])
return

Expand All @@ -112,17 +104,11 @@ def get_all_unittest_file():
for idx, case in enumerate(SKIP_UNITTEST):
logger.info("--> {}. {}".format(idx+1, case))

logger.info(
"\nCPU并行的单测用例:{}个".format(len(CPU_PARALLEL_RUN_UNITTEST)))
logger.info("\nCPU并行的单测用例:{}个".format(len(CPU_PARALLEL_RUN_UNITTEST)))
for idx, case in enumerate(CPU_PARALLEL_RUN_UNITTEST):
logger.info("--> {}. {}".format(idx+1, case))

logger.info(
"\nCPU串行执行的单测用例:{}个".format(len(CPU_SERIAL_RUN_UNITTEST)))
for idx, case in enumerate(CPU_SERIAL_RUN_UNITTEST):
logger.info("--> {}. {}".format(idx+1, case))

logger.info("\n运行模式未知,串行执行的单测用例:{}个".format(len(UNKNOWN_UNITTEST)))
logger.info("\nCPU串行执行的单测用例:{}个".format(len(UNKNOWN_UNITTEST)))
for idx, case in enumerate(UNKNOWN_UNITTEST):
logger.info("--> {}. {}".format(idx+1, case))

Expand Down Expand Up @@ -278,31 +264,18 @@ def run_cpu_parallel_unittest():

return success_cases, failed_cases, end_time - begin_time



def run_cpu_serial_unittest():
"""
运行CPU_SERIAL模式下的单元测试,包括并行和串行两种方式,记录并打印成功和失败的情况及耗时
Args:
Returns:
success_cases (list): 成功运行的测试用例列表
failed_cases (list): 失败运行的测试用例列表
total_time (float): 运行总耗时(单位:秒)
"""
def run_unknown_unittest():
os.environ["TEST_CASE"] = "CPU_SERIAL"
logger.info("\n================ CPU_SERIAL ================\n")

begin_time = time.time()
success_cases, failed_cases, total_case_time = parallel_execute_unittest(
CPU_SERIAL_RUN_UNITTEST, 1)
UNKNOWN_UNITTEST, 1)

logger.info("\n CPU_SERIAL 运行成功单测:{} 个".format(len(success_cases)))

if len(failed_cases) > 0:
logger.info("\n以下单测失败,将重试运行 2 次")
logger.info("\n以下单测失败,将重试运行一次")
for case in failed_cases:
logger.info("retry case --> {}".format(case))
retry_success_cases, retry_failed_cases, retry_case_time = parallel_execute_unittest(
Expand All @@ -313,16 +286,6 @@ def run_cpu_serial_unittest():
for success in retry_success_cases:
failed_cases.remove(success)

if len(retry_failed_cases) > 0:
logger.info("\n以下单测失败,将重试运行 1 次")
for case in retry_failed_cases:
logger.info("retry case --> {}".format(case))
second_success_cases, second_failed_cases, second_case_time = parallel_execute_unittest(
retry_failed_cases, 1)
total_case_time += second_case_time
for success in second_success_cases:
failed_cases.remove(success)

end_time = time.time()
logger.info("\n CPU_SERIAL 运行失败单测: {} 个".format(len(failed_cases)))
for failed in failed_cases:
Expand All @@ -336,41 +299,6 @@ def run_cpu_serial_unittest():
return success_cases, failed_cases, end_time - begin_time


def run_unknown_unittest():
os.environ["TEST_CASE"] = "UNKNOWN"
logger.info("\n================ UNKNOWN ================\n")

begin_time = time.time()
success_cases, failed_cases, total_case_time = parallel_execute_unittest(
UNKNOWN_UNITTEST, 2)

logger.info("\n UNKNOWN 运行成功单测:{} 个".format(len(success_cases)))

if len(failed_cases) > 0:
logger.info("\n以下单测失败,将重试运行一次")
for case in failed_cases:
logger.info("retry case --> {}".format(case))
retry_success_cases, retry_failed_cases, retry_case_time = parallel_execute_unittest(
failed_cases, 1)

total_case_time += retry_case_time

for success in retry_success_cases:
failed_cases.remove(success)

end_time = time.time()
logger.info("\n UNKNOWN 运行失败单测: {} 个".format(len(failed_cases)))
for failed in failed_cases:
logger.info("--> {}".format(failed))

logger.info("\n UNKNOWN 单测并行运行总计耗时 {} s".format(
end_time - begin_time))
logger.info("\n UNKNOWN 单测串行运行总计耗时 {} s".format(
total_case_time))

return success_cases, failed_cases, end_time - begin_time


def create_unittest_report():
"""
生成单元测试报告。
Expand All @@ -391,8 +319,7 @@ def create_unittest_report():
total_failed_cases = []
total_ut_time = 0

test_suite = [run_cpu_parallel_unittest,
run_cpu_serial_unittest, run_unknown_unittest]
test_suite = [run_cpu_parallel_unittest, run_unknown_unittest]
for suite in test_suite:
success_cases, failed_cases, suite_time = suite()
total_success_cases += success_cases
Expand Down
2 changes: 0 additions & 2 deletions python/tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
AppBuilderClient
)


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAgentRuntime(unittest.TestCase):
def setUp(self):
"""
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_animal_recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import appbuilder
import os

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAnimalRecognition(unittest.TestCase):
def setUp(self):
"""
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_appbuilder_assistant_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def tool_calls(self, status_event):
]
)

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppBuilderTrace(unittest.TestCase):
def setUp(self):
"""
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_appbuilder_client_chatflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from appbuilder.core.console.appbuilder_client import data_class


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppBuilderClientChatflow(unittest.TestCase):
def setUp(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def run(self, query=None):
)


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppBuilderClientChatflow(unittest.TestCase):
def setUp(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def gen_action(self):
yield self._create_action()


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppBuilderClientChatflow(unittest.TestCase):
def setUp(self):
"""
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_appbuilder_client_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from appbuilder.utils.trace.phoenix_wrapper import runtime_main,stop_phoenix,launch_phoenix
from appbuilder.core.console.appbuilder_client import get_app_list

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppBuilderTrace(unittest.TestCase):
def setUp(self):
"""
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_appbuilder_components_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
'时候也需特别注意火侯,这样吃起来才会有外脆内Q的口感!')
TEST_ANSWER = '澳门新麻蒲烤肉店并不是每天开门,周日休息。'

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppBuilderComponentsTrace(unittest.TestCase):
def setUp(self):
"""
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_appbuilder_core_components_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from appbuilder.core.components.retriever.baidu_vdb.component import _try_import,BaiduVDBVectorStoreIndex


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppbuilderCoreComponentsRetriever__try_import(unittest.TestCase):
def test_baidu_vdb_baiduvdb_retriever_try_import(self):
subprocess.check_call([sys.executable, "-m", "pip", "uninstall", "-y", "pymochow"])
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_appbuilder_sentry_trace_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

logging.basicConfig(level=logging.INFO)

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppbuilderForSentryOff(unittest.TestCase):

def test_sentry_inport_error(self):
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_appbuilder_sentry_trace_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

logging.basicConfig(level=logging.INFO)

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppbuilderForSentryOff(unittest.TestCase):
def test_sentry_normal(self):
"""
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_appbuilder_trace_raise_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from appbuilder.utils.trace._function import _input,_client_trace_generator,_assistant_stream_run_with_handler_output


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAppbuilderTraceRaiseError(unittest.TestCase):
def setUp(self):
tracer_provider = trace.get_tracer_provider()
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_basic_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import appbuilder

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAssistantImport(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_class_assistans.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import appbuilder
from tests.pytest_utils import Utils

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAssistant(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_class_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from appbuilder.core._exception import AssistantServerException
from tests.pytest_utils import Utils

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestFilesCreate(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_class_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from appbuilder.core._exception import AssistantServerException

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestMessageCreate(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_class_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def get_data_file(filename):
def get_cur_whether(location:str, unit:str):
return "{} 的当前温度是30 {}".format(location, unit)

# @unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestFunctionCall(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
6 changes: 1 addition & 5 deletions python/tests/test_assistant_class_runs_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import appbuilder

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")

def get_cur_whether(location:str, unit:str):
return "{} 的当前温度是30 {}".format(location, unit)

Expand Down Expand Up @@ -61,10 +61,6 @@ def test_run_step_list_v1(self):
step_id=last_step_id,
)
self.assertEqual(step.id, last_step_id)






if __name__ == '__main__':
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_class_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import appbuilder

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestThreadCreate(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_e2e_funccall.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def get_cur_whether(location:str, unit:str):
}
}

# @unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
@unittest.skip(reason="暂时跳过")
class TestFunctionCall(unittest.TestCase):
def setUp(self):
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_e2e_funccall_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import appbuilder


# @unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
@unittest.skip(reason="暂时跳过")
class TestFunctionCall(unittest.TestCase):
def setUp(self):
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_e2e_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import appbuilder

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAssistantTalk(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_e2e_stream_cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def get_cur_whether(location:str, unit:str):
}
}

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestCancel(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_e2e_stream_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def tool_calls(self, status_event):
)


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestFunctionCall(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_e2e_stream_event_handler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def tool_calls(self, status_event):
)


@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestFunctionCall(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_e2e_stream_funccall.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def get_cur_whether(location:str, unit:str):
}
}

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestFunctionCall(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_assistant_e2e_stream_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import time
import appbuilder

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestAssistantStreamTalk(unittest.TestCase):
def setUp(self):
os.environ["APPBUILDER_TOKEN"] = os.environ["APPBUILDER_TOKEN_V2"]
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_console_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from appbuilder.core.console.rag.rag import RAG

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestRag(unittest.TestCase):

def setUp(self):
Expand Down
1 change: 0 additions & 1 deletion python/tests/test_core_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def __init__(self, status_code, headers, text):
def json(self):
return json.loads(self.text)

@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_SERIAL", "")
class TestCoreClient(unittest.TestCase):
def setUp(self):
# 保存原始环境变量
Expand Down
Loading

0 comments on commit eaabe80

Please sign in to comment.