Skip to content
Snippets Groups Projects
Commit f514de52 authored by Eliott Sammier's avatar Eliott Sammier
Browse files

Refactor to use subtests instead of test suites

parent 81ebc110
Branches
No related tags found
Loading
......@@ -8,19 +8,17 @@ TESTS_DIR = Path(RESULT_DIR + "/../tests").resolve()
class CompareFiles(unittest.TestCase):
"""A test case that takes a file from `TESTS_DIR`, finds the corresponding
"""A test case that checks every file in `TESTS_DIR`, finds the corresponding
file in the generated results (in `RESULT_DIR`) and compares them using
`diff`, printing the full diff if they're not identical.
"""
`diff`, printing the full diff if they're not identical."""
def __init__(self, expected: Path) -> None:
super().__init__()
self.expected = expected
def runTest(self):
def runTest(self) -> None:
for file in TESTS_DIR.rglob("*"): # Recursively find all files
if file.is_file():
with self.subTest():
# ex: for a file `TESTS_DIR/pg60/index.md`, will find the first file in
# `RESULT_DIR` whose path ends with `pg60/index.md`
relative = self.expected.relative_to(TESTS_DIR)
relative = file.relative_to(TESTS_DIR)
cmd = subprocess.run(
[
"find",
......@@ -35,26 +33,14 @@ class CompareFiles(unittest.TestCase):
actual = cmd.stdout.splitlines()[0]
# Any exceptions in the lookup above cause the test case to fail, this
# is what we want.
self.compare_files(str(file), actual)
def compare_files(self, expected: str, actual: str):
# Run the diff between the two files, without `capture_output=True`,
# therefore the diff is printed to the console
diffcmd = subprocess.run(
["diff", "--color=always", "-u", self.expected, actual]
)
diffcmd = subprocess.run(["diff", "--color=always", "-u", expected, actual])
self.assertEqual(diffcmd.returncode, 0, "see diff output above.")
class ConversionTests(unittest.TestSuite):
"""A test suite that checks all files in the tests directory, and tries
to compare it with its generated counterpart"""
def __init__(self, tests) -> None:
super().__init__(tests)
for file in TESTS_DIR.rglob("*"): # Recursively find all files
if file.is_file():
self.addTest(CompareFiles(file))
if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(ConversionTests([]))
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment