|
| 1 | +import re |
1 | 2 | import unittest.mock
|
| 3 | +from typing import Pattern |
2 | 4 |
|
3 | 5 | from _pytest.unittest import TestCaseFunction, UnitTestCase
|
4 | 6 |
|
| 7 | +from torch.testing._internal.common_device_type import get_device_type_test_bases |
5 | 8 | from torch.testing._internal.common_utils import TestCase as PyTorchTestCaseTemplate
|
6 | 9 |
|
7 | 10 |
|
| 11 | +class PytestPyTorchInternalError(Exception): |
| 12 | + def __init__(self, msg): |
| 13 | + super().__init__( |
| 14 | + f"{msg}\n" |
| 15 | + f"This is an internal error of the pytest plugin 'pytest-pytorch'." |
| 16 | + f"If you encounter this during normal operation, please file an issue " |
| 17 | + f"https://github.com/Quansight/pytest-pytorch/issues." |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +TEST_BASE_DEVICE_PATTERN = re.compile(r"(?P<device>\w*?)TestBase$") |
| 22 | + |
| 23 | + |
| 24 | +def _get_devices(): |
| 25 | + devices = [] |
| 26 | + for test_base in get_device_type_test_bases(): |
| 27 | + match = TEST_BASE_DEVICE_PATTERN.match(test_base.__name__) |
| 28 | + if not match: |
| 29 | + raise PytestPyTorchInternalError( |
| 30 | + f"Unable to extract device name from {test_base.__name__}" |
| 31 | + ) |
| 32 | + |
| 33 | + devices.append(match.group("device")) |
| 34 | + |
| 35 | + return devices |
| 36 | + |
| 37 | + |
| 38 | +DEVICES = _get_devices() |
| 39 | + |
| 40 | + |
8 | 41 | class TemplateName(str):
|
| 42 | + _TEMPLATE_NAME_PATTERN: Pattern |
| 43 | + |
| 44 | + def __init__(self, _): |
| 45 | + super().__init__() |
| 46 | + match = self._TEMPLATE_NAME_PATTERN.match(self) |
| 47 | + if not match: |
| 48 | + raise PytestPyTorchInternalError( |
| 49 | + f"Unable to extract template name from {self}" |
| 50 | + ) |
| 51 | + self._template_name = match.group("template_name") |
| 52 | + |
9 | 53 | def __eq__(self, other):
|
10 |
| - return self.startswith(str(other)) |
| 54 | + return str.__eq__(self, other) or str.__eq__(self._template_name, other) |
11 | 55 |
|
12 | 56 | def __hash__(self) -> int:
|
13 | 57 | return super().__hash__()
|
14 | 58 |
|
15 | 59 |
|
| 60 | +class TestCaseFunctionTemplateName(TemplateName): |
| 61 | + _TEMPLATE_NAME_PATTERN = re.compile( |
| 62 | + fr"(?P<template_name>\w*?)_({'|'.join([device.lower() for device in DEVICES])})" |
| 63 | + ) |
| 64 | + |
| 65 | + |
16 | 66 | class PyTorchTestCaseFunction(TestCaseFunction):
|
17 | 67 | @classmethod
|
18 | 68 | def from_parent(cls, parent, *, name, **kw):
|
19 |
| - return super().from_parent(parent, name=TemplateName(name), **kw) |
| 69 | + return super().from_parent( |
| 70 | + parent, name=TestCaseFunctionTemplateName(name), **kw |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +class TestCaseTemplateName(TemplateName): |
| 75 | + _TEMPLATE_NAME_PATTERN = re.compile( |
| 76 | + fr"(?P<template_name>\w*?)({'|'.join([device.upper() for device in DEVICES])})" |
| 77 | + ) |
20 | 78 |
|
21 | 79 |
|
22 | 80 | class PyTorchTestCase(UnitTestCase):
|
23 | 81 | @classmethod
|
24 | 82 | def from_parent(cls, parent, *, name, obj=None):
|
25 |
| - return super().from_parent(parent, name=TemplateName(name), obj=obj) |
| 83 | + return super().from_parent(parent, name=TestCaseTemplateName(name), obj=obj) |
26 | 84 |
|
27 | 85 | def collect(self):
|
28 | 86 | # Yes, this is a bad practice. Unfortunately, there is no other option to
|
|
0 commit comments