| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Functions and classes for testing.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import unittest |
| 6 |
|
| 7 |
|
| 8 |
# The path to top of the dfWinReg source tree. |
| 9 |
PROJECT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 10 |
|
| 11 |
# The paths below are all derived from the project path directory. |
| 12 |
# They are enumerated explicitly here so that they can be overwritten for |
| 13 |
# compatibility with different build systems. |
| 14 |
TEST_DATA_PATH = os.path.join(PROJECT_PATH, 'test_data') |
| 15 |
|
| 16 |
|
| 17 |
class BaseTestCase(unittest.TestCase): |
| 18 |
"""The base test case.""" |
| 19 |
|
| 20 |
# Show full diff results. |
| 21 |
maxDiff = None |
| 22 |
|
| 23 |
def _GetTestFilePath(self, path_segments): |
| 24 |
"""Retrieves the path of a test file relative to the test data directory. |
| 25 |
|
| 26 |
Args: |
| 27 |
path_segments (list[str]): path segments inside the test data directory. |
| 28 |
|
| 29 |
Returns: |
| 30 |
str: path of the test file. |
| 31 |
""" |
| 32 |
# Note that we need to pass the individual path segments to os.path.join |
| 33 |
# and not a list. |
| 34 |
return os.path.join(TEST_DATA_PATH, *path_segments) |
| 35 |
|
| 36 |
def _SkipIfPathNotExists(self, path): |
| 37 |
"""Skips the test if the path does not exist. |
| 38 |
|
| 39 |
Args: |
| 40 |
path (str): path of a test file. |
| 41 |
|
| 42 |
Raises: |
| 43 |
SkipTest: if the path does not exist and the test should be skipped. |
| 44 |
""" |
| 45 |
if not os.path.exists(path): |
| 46 |
filename = os.path.basename(path) |
| 47 |
raise unittest.SkipTest(f'missing test file: {filename:s}') |