openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.67.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/conftest.py

51lines · modeblame

98d779fbStainless Bot2 years ago1from __future__ import annotations
2
3import os
08b8179aDavid Schnurr2 years ago4import logging
98d779fbStainless Bot2 years ago5from typing import TYPE_CHECKING, Iterator, AsyncIterator
08b8179aDavid Schnurr2 years ago6
7import pytest
44ac1cefStainless Bot1 years ago8from pytest_asyncio import is_async_test
08b8179aDavid Schnurr2 years ago9
98d779fbStainless Bot2 years ago10from openai import OpenAI, AsyncOpenAI
11
12if TYPE_CHECKING:
13from _pytest.fixtures import FixtureRequest
14
08b8179aDavid Schnurr2 years ago15pytest.register_assert_rewrite("tests.utils")
16
17logging.getLogger("openai").setLevel(logging.DEBUG)
18
19
44ac1cefStainless Bot1 years ago20# 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:
23pytest_asyncio_tests = (item for item in items if is_async_test(item))
24session_scope_marker = pytest.mark.asyncio(loop_scope="session")
25for async_test in pytest_asyncio_tests:
26async_test.add_marker(session_scope_marker, append=False)
98d779fbStainless Bot2 years ago27
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]:
36strict = getattr(request, "param", True)
37if not isinstance(strict, bool):
38raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
39
40with OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client:
41yield client
42
43
44@pytest.fixture(scope="session")
45async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncOpenAI]:
46strict = getattr(request, "param", True)
47if not isinstance(strict, bool):
48raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
49
50async with AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client:
51yield client