Skip to content

Commit 99a597e

Browse files
author
Jussi Kukkonen
committed
tests: Add smart output to aggregate_tests.py
Fix the aggregate_tests.py TextTestRunner buffering so that output from failing tests is now printed below the failure. This is done by adding a logging handler that allows TextTestRunner to modify sys.stderr value between tests. aggregate_tests.py now accepts the same verbosity flag as individual test files (e.g. '-vvv' for DEBUG level). tox sets '-vvv' so failing tests will now get DEBUG output: this probably makes sense on CI at least. Fixes #1120 Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent 647e3e0 commit 99a597e

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

tests/aggregate_tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import glob
4040
import random
4141

42+
import utils
43+
4244
# Generate a list of pathnames that match a pattern (i.e., that begin with
4345
# 'test_' and end with '.py'. A shell-style wildcard is used with glob() to
4446
# match desired filenames. All the tests matching the pattern will be loaded
@@ -90,6 +92,7 @@
9092
random.shuffle(test_modules_to_run)
9193

9294
if __name__ == '__main__':
95+
utils.configure_test_logging(sys.argv)
9396
suite = unittest.TestLoader().loadTestsFromNames(test_modules_to_run)
9497
all_tests_passed = unittest.TextTestRunner(
9598
verbosity=1, buffer=True).run(suite).wasSuccessful()

tests/utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,29 @@
2424
import errno
2525
import logging
2626
import socket
27+
import sys
2728
import time
2829

2930
import tuf.log
3031

3132
logger = logging.getLogger(__name__)
3233

34+
# Logging handler that always checks sys.stderr value when it needs the stream:
35+
# the default handler caches the sys.stderr value and that breaks TextTestRunner
36+
# buffering (it redirects stderr to a new file object for each individual test)
37+
class TestLogHandler(logging.StreamHandler):
38+
def __init__(self):
39+
super().__init__()
40+
41+
@property
42+
def stream(self):
43+
return sys.stderr
44+
45+
@stream.setter
46+
def stream(self, value):
47+
pass
48+
49+
3350
try:
3451
# is defined in Python 3
3552
TimeoutError
@@ -93,5 +110,5 @@ def configure_test_logging(argv):
93110
else:
94111
loglevel = logging.DEBUG
95112

96-
logging.basicConfig()
113+
logging.basicConfig(handlers=[TestLogHandler()])
97114
tuf.log.set_log_level(loglevel)

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ changedir = tests
1616
commands =
1717
pylint {toxinidir}/tuf
1818
bandit -r {toxinidir}/tuf
19-
coverage run aggregate_tests.py
19+
coverage run aggregate_tests.py -vvv
2020
coverage report -m --fail-under 97
2121

2222
deps =
@@ -37,5 +37,5 @@ deps =
3737
--editable {toxinidir}
3838

3939
commands =
40-
coverage run aggregate_tests.py
40+
coverage run aggregate_tests.py -vvv
4141
coverage report -m

0 commit comments

Comments
 (0)