Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions google/genai/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.'
)
Loading