Skip to content

Commit

Permalink
Merge pull request #348 from Yelp/logging-level-to-match-verbosity
Browse files Browse the repository at this point in the history
logging level debug only if verbose
  • Loading branch information
StefanoChiodino authored Sep 29, 2023
2 parents 50045df + 8fe3be9 commit 756a6b7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
45 changes: 21 additions & 24 deletions test/test_case_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,30 +378,27 @@ def test_class_with_two_failing_teardown_methods(self):

def test_multiple_error_formatting(self):
test_result = self.testcase.results()[0]
assert_equal(
test_result.format_exception_info().split('\n'),
[
'Traceback (most recent call last):',
RegexMatcher(r' File "(\./)?test/test_case_test\.py", line \d+, in test_method'),
' assert False',
'AssertionError',
'',
'During handling of the above exception, another exception occurred:',
'',
'Traceback (most recent call last):',
RegexMatcher(r' File "(\./)?test/test_case_test\.py", line \d+, in first_teardown'),
' assert False',
'AssertionError',
'',
'During handling of the above exception, another exception occurred:',
'',
'Traceback (most recent call last):',
RegexMatcher(r' File "(\./)?test/test_case_test\.py", line \d+, in second_teardown'),
' assert False',
'AssertionError',
'', # Ends with newline.
]
)
actual = test_result.format_exception_info()
expected_regex = r"""Traceback \(most recent call last\):
File \".*test\/test_case_test.py\", line \d+, in test_method
assert False
AssertionError
During handling of the above exception, another exception occurred:
Traceback \(most recent call last\):
File \".*test\/test_case_test.py\", line \d+, in first_teardown
assert False
AssertionError
During handling of the above exception, another exception occurred:
Traceback \(most recent call last\):
File \".*test\/test_case_test.py\", line \d+, in second_teardown
assert False
AssertionError
"""
assert_equal(RegexMatcher(expected_regex), actual)


class RegexMatcher(object):
Expand Down
14 changes: 14 additions & 0 deletions test/test_program_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,17 @@ def test_rerun_with_failure_limit(self):
b'test.fails_two_tests FailsTwoTests.test2\n'
)
assert_in(b'FAILED. 1 test / 1 case: 0 passed, 1 failed.', stdout)


class LoggingLevelTest(TestCase):
def test_default_logging_level(self):
_, _, _, options = test_program.parse_test_runner_command_line_args([], ["path"])
with mock.patch("testify.test_program.logging") as logging_mock:
test_program.TestProgram().setup_logging(options)
logging_mock.getLogger.return_value.setLevel.assert_not_called()

def test_varbose_logging_level(self):
_, _, _, options = test_program.parse_test_runner_command_line_args([], ["path", "--verbose"])
with mock.patch("testify.test_program.logging") as logging_mock:
test_program.TestProgram().setup_logging(options)
logging_mock.getLogger.return_value.setLevel.assert_called_with(logging_mock.DEBUG)
11 changes: 9 additions & 2 deletions testify/test_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ def default_parser():

parser.set_defaults(verbosity=test_logger.VERBOSITY_NORMAL)
parser.add_option("-s", "--silent", action="store_const", const=test_logger.VERBOSITY_SILENT, dest="verbosity")
parser.add_option("-v", "--verbose", action="store_const", const=test_logger.VERBOSITY_VERBOSE, dest="verbosity")
parser.add_option(
"-v", "--verbose",
action="store_const",
const=test_logger.VERBOSITY_VERBOSE,
dest="verbosity",
help="Outputs a more verbose output and sets the root logger level to debug."
)
parser.add_option(
'-d', '--ipdb', '--pdb',
action="store_true",
Expand Down Expand Up @@ -317,7 +323,8 @@ def run(self):

def setup_logging(self, options):
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
if options.verbosity == test_logger.VERBOSITY_VERBOSE:
root_logger.setLevel(logging.DEBUG)

console = logging.StreamHandler()
console.setFormatter(logging.Formatter("%(levelname)-8s %(message)s"))
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py27,py34,py35
envlist = py27,py34,py35,py310

[testenv]
passenv = TERM
Expand Down

0 comments on commit 756a6b7

Please sign in to comment.