|
1 | 1 | import sys
|
2 | 2 | import unittest
|
| 3 | +import warnings |
3 | 4 | from itertools import groupby
|
4 | 5 |
|
5 | 6 | # Use timeit.default_timer for Python 2 compatibility.
|
|
10 | 11 |
|
11 | 12 |
|
12 | 13 | class CodewarsTestRunner(object):
|
13 |
| - def __init__(self, stream=None, group_by_module=False): |
| 14 | + def __init__(self, stream=None, group_by_module=False, warnings=None): |
14 | 15 | if stream is None:
|
15 | 16 | stream = sys.stdout
|
16 | 17 | self.stream = _WritelnDecorator(stream)
|
17 | 18 | self.group_by_module = group_by_module
|
18 | 19 | self.results = []
|
| 20 | + if warnings is None and not sys.warnoptions: |
| 21 | + self.warnings = "default" |
| 22 | + else: |
| 23 | + # Set to given `warnings` or use `None` to respect values passed to `-W` |
| 24 | + self.warnings = warnings |
19 | 25 |
|
20 | 26 | def run(self, test):
|
21 |
| - if isinstance(test, unittest.TestSuite): |
22 |
| - self._run_modules(_to_tree(_flatten(test))) |
23 |
| - else: |
24 |
| - self._run_case(test) |
| 27 | + with warnings.catch_warnings(): |
| 28 | + if self.warnings: |
| 29 | + warnings.simplefilter(self.warnings) |
| 30 | + # Minimize noise from deprecated assertion methods |
| 31 | + if self.warnings in ["default", "always"]: |
| 32 | + warnings.filterwarnings( |
| 33 | + "module", |
| 34 | + category=DeprecationWarning, |
| 35 | + message=r"Please use assert\w+ instead.", |
| 36 | + ) |
| 37 | + if isinstance(test, unittest.TestSuite): |
| 38 | + self._run_modules(_to_tree(_flatten(test))) |
| 39 | + else: |
| 40 | + self._run_case(test) |
25 | 41 | return self._make_result()
|
26 | 42 |
|
27 | 43 | def _make_result(self):
|
|
0 commit comments