openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dfdcf571ced31f7859cd1871be39e2fb3af6bafa

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/conftest.py

51lines · modecode

1from __future__ import annotations
2
3import os
4import logging
5from typing import TYPE_CHECKING, Iterator, AsyncIterator
6
7import pytest
8from pytest_asyncio import is_async_test
9
10from openai import OpenAI, AsyncOpenAI
11
12if TYPE_CHECKING:
13 from _pytest.fixtures import FixtureRequest
14
15pytest.register_assert_rewrite("tests.utils")
16
17logging.getLogger("openai").setLevel(logging.DEBUG)
18
19
20# automatically add `pytest.mark.asyncio()` to all of our async tests
21# so we don't have to add that boilerplate everywhere
22def pytest_collection_modifyitems(items: list[pytest.Function]) -> None:
23 pytest_asyncio_tests = (item for item in items if is_async_test(item))
24 session_scope_marker = pytest.mark.asyncio(loop_scope="session")
25 for async_test in pytest_asyncio_tests:
26 async_test.add_marker(session_scope_marker, append=False)
27
28
29base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
30
31api_key = "My API Key"
32
33
34@pytest.fixture(scope="session")
35def client(request: FixtureRequest) -> Iterator[OpenAI]:
36 strict = getattr(request, "param", True)
37 if not isinstance(strict, bool):
38 raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
39
40 with OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client:
41 yield client
42
43
44@pytest.fixture(scope="session")
45async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncOpenAI]:
46 strict = getattr(request, "param", True)
47 if not isinstance(strict, bool):
48 raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
49
50 async with AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client:
51 yield client
52