Make sure that fallback values for log-file formats and level works.
(pytester: Pytester)
| 1523 | |
| 1524 | |
| 1525 | def test_log_file_cli_fallback_options(pytester: Pytester) -> None: |
| 1526 | """Make sure that fallback values for log-file formats and level works.""" |
| 1527 | pytester.makepyfile( |
| 1528 | """ |
| 1529 | import logging |
| 1530 | logger = logging.getLogger() |
| 1531 | |
| 1532 | def test_foo(): |
| 1533 | logger.info('info text going to logger') |
| 1534 | logger.warning('warning text going to logger') |
| 1535 | logger.error('error text going to logger') |
| 1536 | |
| 1537 | assert 0 |
| 1538 | """ |
| 1539 | ) |
| 1540 | log_file = str(pytester.path.joinpath("pytest.log")) |
| 1541 | result = pytester.runpytest( |
| 1542 | "--log-level=ERROR", |
| 1543 | "--log-format=%(asctime)s %(message)s", |
| 1544 | "--log-date-format=%H:%M", |
| 1545 | "--log-file=pytest.log", |
| 1546 | ) |
| 1547 | assert result.ret == 1 |
| 1548 | |
| 1549 | # The log file should only contain the error log messages |
| 1550 | # not the warning or info ones and the format and date format |
| 1551 | # should match the formats provided using --log-format and --log-date-format |
| 1552 | assert os.path.isfile(log_file) |
| 1553 | with open(log_file, encoding="utf-8") as rfh: |
| 1554 | contents = rfh.read() |
| 1555 | assert re.match(r"[0-9]{2}:[0-9]{2} error text going to logger\s*", contents) |
| 1556 | assert "info text going to logger" not in contents |
| 1557 | assert "warning text going to logger" not in contents |
| 1558 | assert "error text going to logger" in contents |
| 1559 | |
| 1560 | # Try with a different format and date format to make sure that the formats |
| 1561 | # are being used |
| 1562 | result = pytester.runpytest( |
| 1563 | "--log-level=ERROR", |
| 1564 | "--log-format=%(asctime)s : %(message)s", |
| 1565 | "--log-date-format=%H:%M:%S", |
| 1566 | "--log-file=pytest.log", |
| 1567 | ) |
| 1568 | assert result.ret == 1 |
| 1569 | |
| 1570 | # The log file should only contain the error log messages |
| 1571 | # not the warning or info ones and the format and date format |
| 1572 | # should match the formats provided using --log-format and --log-date-format |
| 1573 | assert os.path.isfile(log_file) |
| 1574 | with open(log_file, encoding="utf-8") as rfh: |
| 1575 | contents = rfh.read() |
| 1576 | assert re.match( |
| 1577 | r"[0-9]{2}:[0-9]{2}:[0-9]{2} : error text going to logger\s*", contents |
| 1578 | ) |
| 1579 | assert "info text going to logger" not in contents |
| 1580 | assert "warning text going to logger" not in contents |
| 1581 | assert "error text going to logger" in contents |
nothing calls this directly
no test coverage detected
searching dependent graphs…