-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathconftest.py
79 lines (64 loc) · 2.31 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from unittest.mock import patch
import pytest
from dotenv import load_dotenv
from kiln_ai.utils.config import Config
@pytest.fixture(scope="session", autouse=True)
def load_env():
load_dotenv()
# mock out the settings path so we don't clobber the user's actual settings during tests
@pytest.fixture(autouse=True)
def use_temp_settings_dir(tmp_path):
with patch.object(
Config, "settings_path", return_value=str(tmp_path / "settings.yaml")
):
yield
def pytest_addoption(parser):
parser.addoption(
"--runpaid",
action="store_true",
default=False,
help="run tests that make paid API calls",
)
parser.addoption(
"--runsinglewithoutchecks",
action="store_true",
default=False,
help="if testing a single test, don't check for skips like runpaid",
)
parser.addoption(
"--ollama",
action="store_true",
default=False,
help="run tests that use ollama server",
)
def is_single_manual_test(config, items) -> bool:
# Check if we're running manually (eg, in vscode)
if not config.getoption("--runsinglewithoutchecks"):
return False
if len(items) == 1:
return True
if len(items) == 0:
return False
# Check if all of the items are the same prefix, expluding a.b.c[param]
# This is still a 'single test' for the purposes of this flag
prefix = items[0].name.split("[")[0] + "["
for item in items:
if not item.name.startswith(prefix):
return False
return True
def pytest_collection_modifyitems(config, items):
# Always run test if it's a single test manually invoked
if is_single_manual_test(config, items):
return
# Mark tests that use paid services as skipped unless --runpaid is passed
if not config.getoption("--runpaid"):
skip_paid = pytest.mark.skip(reason="need --runpaid option to run")
for item in items:
if "paid" in item.keywords:
item.add_marker(skip_paid)
# Mark tests that use ollama server as skipped unless --ollama is passed
if not config.getoption("--ollama"):
skip_ollama = pytest.mark.skip(reason="need --ollama option to run")
for item in items:
if "ollama" in item.keywords:
item.add_marker(skip_ollama)