Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLDR-646 added progress to tests #436

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ exclude =
# ANN101 - type annotations for self
# T201 - prints found
# JS101 - Multi-line container not broken after opening character
# ANN001 - Missing type annotation for function argument
# ANN201 - Missing return type annotation for public function
# ANN202 - Missing return type annotation for protected function
# ANN204 - Missing return type annotation for special method
# N802 - function name should be lowercase
ignore =
ANN101
per-file-ignores =
scripts/*:T201
scripts/benchmark_pdf_performance*:JS101
tests/custom_test_runner.py:ANN001,ANN201,ANN202,ANN204,N802
NastyBoget marked this conversation as resolved.
Show resolved Hide resolved
60 changes: 60 additions & 0 deletions tests/custom_test_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import itertools
import unittest.runner


class CustomTextTestResult(unittest.runner.TextTestResult):
"""Extension of TextTestResult to support numbering test cases"""

def __init__(self, stream, descriptions, verbosity):
"""Initializes the test number generator, then calls super impl"""

self.test_numbers = itertools.count(1)

return super(CustomTextTestResult, self).__init__(stream, descriptions, verbosity)

def startTest(self, test):
"""Writes the test number to the stream if showAll is set, then calls super impl"""

if self.showAll:
test_numbers = next(self.test_numbers)
test_case_count = self.test_case_count
progress = f"[{test_numbers}/{test_case_count}] "
self.stream.write(progress)

# Also store the progress in the test itself, so that if it errors,
# it can be written to the exception information by our overridden
# _exec_info_to_string method:
test.progress_index = progress

return super(CustomTextTestResult, self).startTest(test)

def _exc_info_to_string(self, err, test):
"""Gets an exception info string from super, and prepends 'Test Number' line"""

info = super(CustomTextTestResult, self)._exc_info_to_string(err, test)

if self.showAll:
index = test.progress_index
test_info = info
info = f"Test number: {index}\n{test_info}"

return info


class CustomTextTestRunner(unittest.runner.TextTestRunner):
"""Extension of TextTestRunner to support numbering test cases"""

resultclass = CustomTextTestResult

def run(self, test):
"""Stores the total count of test cases, then calls super impl"""

self.test_case_count = test.countTestCases()
return super(CustomTextTestRunner, self).run(test)

def _makeResult(self):
"""Creates and returns a result instance that knows the count of test cases"""

result = super(CustomTextTestRunner, self)._makeResult()
result.test_case_count = self.test_case_count
return result
18 changes: 18 additions & 0 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import unittest

from custom_test_runner import CustomTextTestRunner

TEST_PREFIX = "test_"


def run_test_group(group_name: str) -> None:
runner = CustomTextTestRunner(verbosity=2)
loader = unittest.TestLoader()
test_suite = loader.discover(os.path.join(os.path.dirname(os.path.abspath(__file__)), group_name + "_tests"), pattern=f"{TEST_PREFIX}*.py")
runner.run(test_suite)


if __name__ == "__main__":
for group in ("unit", "api"):
run_test_group(group)
2 changes: 1 addition & 1 deletion tests/run_tests_in_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if [ "$is_test" = "true" ]
apt install -y cowsay
echo "run tests"
sleep 5
python3 -m unittest -v -f /dedoc_root/tests/unit_tests/test* /dedoc_root/tests/api_tests/test_api*
python3 /dedoc_root/tests/run_tests.py
test_exit_code=$?
if [ $test_exit_code -eq 0 ]
then /usr/games/cowsay "all right"
Expand Down
Loading