@@ -77,14 +77,14 @@ def test_foo():
77
77
assert "warning text going to logger" not in stdout
78
78
assert "info text going to logger" not in stdout
79
79
80
- # The log file should contain the warning and the error log messages and
81
- # not the info one , because the default level of the root logger is
82
- # WARNING .
80
+ # The log file should only contain the error log messages and
81
+ # not the warning or info ones , because the root logger is set to
82
+ # ERROR using --log-level=ERROR .
83
83
assert os .path .isfile (log_file )
84
84
with open (log_file , encoding = "utf-8" ) as rfh :
85
85
contents = rfh .read ()
86
86
assert "info text going to logger" not in contents
87
- assert "warning text going to logger" in contents
87
+ assert "warning text going to logger" not in contents
88
88
assert "error text going to logger" in contents
89
89
90
90
@@ -1331,3 +1331,62 @@ def test_foo():
1331
1331
result .stdout .re_match_lines (
1332
1332
[r"^[0-9-]{10} [0-9:]{8}.[0-9]{6}[+-][0-9\.]+; WARNING; text" ]
1333
1333
)
1334
+
1335
+
1336
+ def test_log_file_cli_fallback_options (pytester : Pytester ) -> None :
1337
+ """Make sure that fallback values for log-file formats and level works."""
1338
+ pytester .makepyfile (
1339
+ """
1340
+ import logging
1341
+ logger = logging.getLogger()
1342
+
1343
+ def test_foo():
1344
+ logger.info('info text going to logger')
1345
+ logger.warning('warning text going to logger')
1346
+ logger.error('error text going to logger')
1347
+
1348
+ assert 0
1349
+ """
1350
+ )
1351
+ log_file = str (pytester .path .joinpath ("pytest.log" ))
1352
+ result = pytester .runpytest (
1353
+ "--log-level=ERROR" ,
1354
+ "--log-format=%(asctime)s %(message)s" ,
1355
+ "--log-date-format=%H:%M" ,
1356
+ "--log-file=pytest.log" ,
1357
+ )
1358
+ assert result .ret == 1
1359
+
1360
+ # The log file should only contain the error log messages
1361
+ # not the warning or info ones and the format and date format
1362
+ # should match the formats provided using --log-format and --log-date-format
1363
+ assert os .path .isfile (log_file )
1364
+ with open (log_file , encoding = "utf-8" ) as rfh :
1365
+ contents = rfh .read ()
1366
+ assert re .match (r"[0-9]{2}:[0-9]{2} error text going to logger\s*" , contents )
1367
+ assert "info text going to logger" not in contents
1368
+ assert "warning text going to logger" not in contents
1369
+ assert "error text going to logger" in contents
1370
+
1371
+ # Try with a different format and date format to make sure that the formats
1372
+ # are being used
1373
+ result = pytester .runpytest (
1374
+ "--log-level=ERROR" ,
1375
+ "--log-format=%(asctime)s : %(message)s" ,
1376
+ "--log-date-format=%H:%M:%S" ,
1377
+ "--log-file=pytest.log" ,
1378
+ )
1379
+ assert result .ret == 1
1380
+
1381
+ # The log file should only contain the error log messages
1382
+ # not the warning or info ones and the format and date format
1383
+ # should match the formats provided using --log-format and --log-date-format
1384
+ assert os .path .isfile (log_file )
1385
+ with open (log_file , encoding = "utf-8" ) as rfh :
1386
+ contents = rfh .read ()
1387
+ assert re .match (
1388
+ r"[0-9]{2}:[0-9]{2}:[0-9]{2} : error text going to logger\s*" , contents
1389
+ )
1390
+ assert "info text going to logger" not in contents
1391
+ assert "warning text going to logger" not in contents
1392
+ assert "error text going to logger" in contents
0 commit comments