openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cc2c1fc15fd0bf1a5bdfb7b28b4d8d34e1cccad2

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/conftest.py

53lines · modecode

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