Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6454d34

Browse files
committedMay 13, 2025·
Lib/test/test_logging.py: Make BasicConfigTest cover formatter
1 parent 7a36d67 commit 6454d34

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed
 

‎Lib/test/test_logging.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
import weakref
6262

6363
from http.server import HTTPServer, BaseHTTPRequestHandler
64-
from unittest.mock import patch
64+
from unittest.mock import call, Mock, patch
6565
from urllib.parse import urlparse, parse_qs
6666
from socketserver import (ThreadingUDPServer, DatagramRequestHandler,
6767
ThreadingTCPServer, StreamRequestHandler)
@@ -5655,12 +5655,19 @@ def test_incompatible(self):
56555655
assertRaises = self.assertRaises
56565656
handlers = [logging.StreamHandler()]
56575657
stream = sys.stderr
5658+
formatter = logging.Formatter()
56585659
assertRaises(ValueError, logging.basicConfig, filename='test.log',
56595660
stream=stream)
56605661
assertRaises(ValueError, logging.basicConfig, filename='test.log',
56615662
handlers=handlers)
56625663
assertRaises(ValueError, logging.basicConfig, stream=stream,
56635664
handlers=handlers)
5665+
assertRaises(ValueError, logging.basicConfig, formatter=formatter,
5666+
format='%(message)s')
5667+
assertRaises(ValueError, logging.basicConfig, formatter=formatter,
5668+
datefmt='%H:%M:%S')
5669+
assertRaises(ValueError, logging.basicConfig, formatter=formatter,
5670+
style='%')
56645671
# Issue 23207: test for invalid kwargs
56655672
assertRaises(ValueError, logging.basicConfig, loglevel=logging.INFO)
56665673
# Should pop both filename and filemode even if filename is None
@@ -5795,6 +5802,20 @@ def dummy_handle_error(record):
57955802
# didn't write anything due to the encoding error
57965803
self.assertEqual(data, r'')
57975804

5805+
def test_formatter_given(self):
5806+
mock_formatter = Mock()
5807+
mock_handler = Mock(formatter=None)
5808+
with patch("logging.Formatter") as mock_formatter_init:
5809+
logging.basicConfig(formatter=mock_formatter, handlers=[mock_handler])
5810+
self.assertEqual(mock_handler.setFormatter.call_args_list, [call(mock_formatter)])
5811+
self.assertEqual(mock_formatter_init.call_count, 0)
5812+
5813+
def test_formatter_not_given(self):
5814+
mock_handler = Mock(formatter=None)
5815+
with patch("logging.Formatter") as mock_formatter_init:
5816+
logging.basicConfig(handlers=[mock_handler])
5817+
self.assertEqual(mock_formatter_init.call_count, 1)
5818+
57985819
@support.requires_working_socket()
57995820
def test_log_taskName(self):
58005821
async def log_record():

0 commit comments

Comments
 (0)
Please sign in to comment.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant