From fc391108da4d157f9d0720e6c7dcf6f70adf52c2 Mon Sep 17 00:00:00 2001 From: Xiaodong Ye Date: Thu, 3 Dec 2026 17:30:50 +0800 Subject: [PATCH] fix(test): make the scripts fail on a failed check `make test` ran `pytest test_*.py`, which collected functions that are not pytest cases (two helpers and six device probes that take their arguments from `main()`), so it stopped with "fixture 'devices' not found": 24 passed, 8 errors. The scripts behind the other CI steps had the opposite problem: they printed every failed API and still exited 0, so a broken binding left CI green. `test_error` and `test_api` counted nothing and their 77 return values were dropped on the floor. - Mark the helpers `__test__ = False` (pytest's documented opt-out), so `make test` is 24 passed. - Count every check in `test_error` / `test_api`, print a summary, and exit 1 when one failed. `Not Supported` is the platform answering rather than a defect, so it counts as skipped and keeps CI green. - Fix the exception contract the pynvml side documents: `NVMLError` was a subclass of `MTMLError`, while `_mtmlCheckReturn` raises `MTMLError`, so `except pynvml.NVMLError` - the pattern `test_pynvml.py` itself uses - caught nothing and fell through to `except Exception`. MANUAL.md already documents `NVMLError = MTMLError`; the code now matches it, which also makes every `NVMLError_*` a subclass of `NVMLError` as pynvml guarantees. Evidence on an MTT S5000 (vllm:v0.32.0-ph1-5.2.0-torch2.13.0): make test 24 passed test_pymtml.py exit 0 646 passed, 120 skipped (not supported), 0 failed test_pynvml.py exit 0 57 passed, 2 skipped (not supported), 0 failed test_sglang_compat.py exit 0 nvmlDeviceGetMaxClockInfo (GRAPHICS): [NVMLError: Not Supported] (not supported) Injecting a RuntimeError into one API makes `main()` return 1, so the gate fires. --- pymtml.py | 14 ++++++-------- test_pymtml.py | 26 +++++++++++++++++++++++++- test_pynvml.py | 24 +++++++++++++++++++++++- test_sglang_compat.py | 14 ++++++++++++++ 4 files changed, 68 insertions(+), 10 deletions(-) diff --git a/pymtml.py b/pymtml.py index 3cfbc15..024e726 100644 --- a/pymtml.py +++ b/pymtml.py @@ -2165,14 +2165,12 @@ class NVMLUtilization: memory: int -class NVMLError(MTMLError): - def __new__(typ, value): - obj = super().__new__(MTMLError, value) - - if not isinstance(obj, NVMLError): - obj.__class__ = NVMLError - - return obj +# pynvml's contract is that every NVMLError_* is an NVMLError, and callers +# write `except NVMLError`. The MTML layer raises MTMLError (and its generated +# subclasses), so an alias - as MANUAL.md documents - is what makes both +# `except NVMLError` and `except MTMLError` catch every failure. A subclass +# here would leave `except NVMLError` catching nothing. +NVMLError = MTMLError def nvmlStructToFriendlyObject(struct): diff --git a/test_pymtml.py b/test_pymtml.py index c4368aa..4ccad17 100644 --- a/test_pymtml.py +++ b/test_pymtml.py @@ -10,6 +10,11 @@ from pymtml import * +# Every check is counted: an API that fails prints a line, and the script must +# still exit non-zero, otherwise CI is green while the binding is broken. +# `Not Supported` is the platform answering, not a defect, so it is a skip. +RESULTS = {"passed": 0, "skipped": 0, "failed": 0} + def print_section(title): print(f"\n{'='*60}") @@ -27,12 +32,19 @@ def test_error(name, func): try: result = func() print_result(name, result) + RESULTS["passed"] += 1 return True + except MTMLError_NotSupported as e: + print_result(name, f"[MTMLError: {e}] (not supported)") + RESULTS["skipped"] += 1 + return False except MTMLError as e: print_result(name, f"[MTMLError: {e}]") + RESULTS["failed"] += 1 return False except Exception as e: print_result(name, f"[Error: {e}]") + RESULTS["failed"] += 1 return False @@ -370,6 +382,12 @@ def run_all_tests(self): print_section("Test Complete") +# `test_error` is a helper called by main(), not a pytest case: pytest would +# collect it and stop with "fixture 'name' not found". `make test` runs +# `pytest test_*.py`, so keep it out of collection. +test_error.__test__ = False + + def test_init_shutdown_cycle(): """Test that library can be initialized and shutdown multiple times.""" print_section("Init/Shutdown Cycle Test") @@ -419,7 +437,13 @@ def main(): traceback.print_exc() return 1 - return 0 + print_section("Result") + print( + f" {RESULTS['passed']} passed, {RESULTS['skipped']} skipped" + f" (not supported), {RESULTS['failed']} failed" + ) + + return 1 if RESULTS["failed"] else 0 if __name__ == "__main__": diff --git a/test_pynvml.py b/test_pynvml.py index 9a7fcef..0010b88 100644 --- a/test_pynvml.py +++ b/test_pynvml.py @@ -12,6 +12,10 @@ # Import pymtml as if it were pynvml import pymtml as pynvml +# See test_pymtml.py: an API that fails must fail the run, and `Not Supported` +# is a platform answer rather than a defect. +RESULTS = {"passed": 0, "skipped": 0, "failed": 0} + def print_section(title): print(f"\n{'='*60}") @@ -29,12 +33,19 @@ def test_api(name, func): try: result = func() print_result(name, result) + RESULTS["passed"] += 1 return True, result + except pynvml.NVMLError_NotSupported as e: + print_result(name, f"[NVMLError: {e}] (not supported)") + RESULTS["skipped"] += 1 + return False, None except pynvml.NVMLError as e: print_result(name, f"[NVMLError: {e}]") + RESULTS["failed"] += 1 return False, None except Exception as e: print_result(name, f"[Error: {e}]") + RESULTS["failed"] += 1 return False, None @@ -394,6 +405,11 @@ def run_all_tests(self): print_section("Test Complete") +# `test_api` is a helper called by main(), not a pytest case: pytest would +# collect it and stop with "fixture 'name' not found". +test_api.__test__ = False + + def main(): try: # Initialize library @@ -420,7 +436,13 @@ def main(): except Exception: pass - return 0 + print_section("Result") + print( + f" {RESULTS['passed']} passed, {RESULTS['skipped']} skipped" + f" (not supported), {RESULTS['failed']} failed" + ) + + return 1 if RESULTS["failed"] else 0 if __name__ == "__main__": diff --git a/test_sglang_compat.py b/test_sglang_compat.py index aa030a3..1d17370 100644 --- a/test_sglang_compat.py +++ b/test_sglang_compat.py @@ -268,6 +268,20 @@ def test_sglang_nvlink_full_connection(physical_device_ids, logger=None): return True +# The probes above take the device list from main(); they are not pytest cases. +# pytest collects a `test_*` function with arguments and stops with +# "fixture 'devices' not found", which is what `make test` reported. +for _probe in ( + test_p2p_status, + test_topology_detection, + test_nvlink_apis, + test_device_info_apis, + test_ecc_apis, + test_sglang_nvlink_full_connection, +): + _probe.__test__ = False + + def main(): print("\n" + "=" * 60) print(" MTML sglang Compatibility Test Suite")