2023-03-31 20:05:14 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from tml.ml_logging.torch_logging import logging
|
|
|
|
|
|
|
|
|
|
|
|
class Testtlogging(unittest.TestCase):
|
|
|
|
def test_warn_once(self):
|
2023-09-13 07:52:13 +02:00
|
|
|
"""
|
|
|
|
Test that warning messages are logged only once when using the assertLogs context manager.
|
|
|
|
|
|
|
|
This unit test checks the behavior of the logging system when warning messages are issued
|
|
|
|
multiple times within the same context. It uses the assertLogs context manager to capture
|
|
|
|
log messages at the INFO level and verifies that warning messages are logged only once.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
To use this test case, call it using a test runner like unittest:
|
|
|
|
```
|
|
|
|
python -m unittest your_test_module.TestLogging.test_warn_once
|
|
|
|
```
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2023-03-31 20:05:14 +02:00
|
|
|
with self.assertLogs(level="INFO") as captured_logs:
|
|
|
|
logging.info("first info")
|
|
|
|
logging.warning("first warning")
|
|
|
|
logging.warning("first warning")
|
|
|
|
logging.info("second info")
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
captured_logs.output,
|
|
|
|
[
|
|
|
|
"INFO:absl:first info",
|
|
|
|
"WARNING:absl:first warning",
|
|
|
|
"INFO:absl:second info",
|
|
|
|
],
|
|
|
|
)
|