From dea104346960d1bbcd436666180a18372b8086d5 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Fri, 23 Oct 2026 11:19:29 -0700 Subject: [PATCH] chore: fail tests that return a value or an unawaited coroutine FUTURE_COPYBARA_INTEGRATE_REVIEW=https://github.com/googleapis/python-genai/pull/3180 from googleapis:release-please--branches--main 4386e38cea5553127ad557ebf8b296e725317fec PiperOrigin-RevId: 1005734684 --- google/genai/tests/conftest.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/google/genai/tests/conftest.py b/google/genai/tests/conftest.py index 4af3c4a65..d7a9c5281 100644 --- a/google/genai/tests/conftest.py +++ b/google/genai/tests/conftest.py @@ -17,7 +17,9 @@ """Conftest for google.genai tests.""" import datetime +import inspect import os +import unittest from unittest import mock import uuid import pytest @@ -240,3 +242,23 @@ def image_png(): image_path = os.path.join(os.path.dirname(__file__), 'data', 'google.png') with PIL.Image.open(image_path) as img: yield img + + +def pytest_runtest_setup(item): + """Errors on `async def` tests that a plain `unittest.TestCase` won't await. + + `unittest.TestCase` calls an `async def` test method, gets back a coroutine, + and reports the test as passed without ever running its body. Only + `unittest.IsolatedAsyncioTestCase` awaits them. + """ + cls = getattr(item, 'cls', None) + if ( + cls is not None + and issubclass(cls, unittest.TestCase) + and not issubclass(cls, unittest.IsolatedAsyncioTestCase) + and inspect.iscoroutinefunction(getattr(item, 'obj', None)) + ): + raise TypeError( + f'{item.nodeid} is `async def` on a plain unittest.TestCase, so it' + ' would never be awaited. Use unittest.IsolatedAsyncioTestCase.' + )