-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
186 lines (145 loc) · 4.75 KB
/
test.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import logging
import re
import unittest.case
from argparse import ArgumentParser
from pathlib import Path
from typing import List, Optional
def set_logger_level(level):
from accelbyte_py_sdk import __name__ as module_name
logging.basicConfig()
logging.getLogger(module_name).setLevel(level)
def reset():
from accelbyte_py_sdk import reset
reset()
def parse_args():
def file_path(value: str) -> Optional[str]:
path = Path(value).resolve()
path_str = str(path)
if not path.exists():
logging.warning(f"FileNotFound: {path_str}")
path_str = None
return path_str
def str_to_bool(value: str) -> bool:
return value.lower() in ["1", "t", "true", "y", "yes"]
parser = ArgumentParser()
parser.add_argument("--test_core", default=True, type=str_to_bool, required=False)
parser.add_argument(
"--test_integration", default=False, type=str_to_bool, required=False
)
parser.add_argument(
"--test_all", action="store_true", default=False, required=False
)
parser.add_argument(
"--dotenv_file",
default="tests/integration/.env",
type=file_path,
required=False,
)
parser.add_argument(
"--use_dotenv", action="store_true", default=False, required=False
)
parser.add_argument(
"-f",
"--filter",
default=None,
type=str,
required=False,
help=f"performs a regex filter on test cases",
)
parser.add_argument(
"-e",
"--exclude",
default=None,
type=str,
required=False,
help=f"performs a regex exclude on test cases.",
)
parser.add_argument("--use_tap", action="store_true", default=False, required=False)
result = vars(parser.parse_args())
if result["test_all"]:
for k, v in result.items():
if not k.startswith("test_"):
continue
result[k] = True
del result["test_all"]
return result
# noinspection PyShadowingBuiltins
def should_skip(
test_case: str, filter: Optional[str] = None, exclude: Optional[str] = None
) -> bool:
if filter is not None and not re.match(filter, test_case):
return True
if exclude is not None and re.match(exclude, test_case):
return True
return False
# noinspection PyShadowingBuiltins
def load_tests_from_module(
loader,
module,
filter: Optional[str] = None,
exclude: Optional[str] = None,
**kwargs,
) -> List[unittest.TestSuite]:
tests = []
for name in dir(module):
obj = getattr(module, name)
if (
isinstance(obj, type)
and obj != unittest.case.TestCase
and issubclass(obj, unittest.case.TestCase)
):
if should_skip(obj.__name__, filter=filter, exclude=exclude):
continue
suite = loader.loadTestsFromTestCase(obj)
tests.append(suite)
tests = loader.suiteClass(tests)
return tests
def main(*args, **kwargs) -> None:
import unittest
loader = unittest.TestLoader()
runner = None
if kwargs.get("use_tap", False):
try:
import tap
runner = tap.TAPTestRunner()
runner.set_stream(True)
except ImportError as e:
pass
if runner is None:
runner = unittest.TextTestRunner()
results = {"test_core": True, "test_integration": True}
if kwargs.get("test_core", False):
import tests.core
import tests.sdk.core
import tests.sdk.api
core_tests = load_tests_from_module(loader, tests.core, **kwargs)
sdk_core_tests = load_tests_from_module(loader, tests.sdk.core, **kwargs)
sdk_api_tests = load_tests_from_module(loader, tests.sdk.api, **kwargs)
suite = unittest.TestSuite(
[
core_tests,
sdk_core_tests,
sdk_api_tests,
]
)
results_core = runner.run(suite)
results["test_core"] = results_core.wasSuccessful()
reset()
if kwargs.get("test_integration", False):
import tests.integration
dotenv_file = kwargs.get("dotenv_file", None)
use_dotenv = kwargs.get("use_dotenv", False)
if dotenv_file and use_dotenv:
tests.integration.DOTENV_FILE = dotenv_file
integration_tests = load_tests_from_module(loader, tests.integration, **kwargs)
suite = unittest.TestSuite(
[
integration_tests,
]
)
results_integration = runner.run(suite)
results["test_integration"] = results_integration.wasSuccessful()
reset()
exit(0 if all(results.values()) else 1)
if __name__ == "__main__":
main(**parse_args())