Skip to content

Commit f9a4961

Browse files
committed
Finished tests for mda.start_logging() and mda.stop_logging()
1 parent c34eca5 commit f9a4961

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

package/MDAnalysis/lib/log.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ def start_logging(stream="MDAnalysis.log", version=version.__version__):
119119
level="INFO",
120120
fmt="%(name)-12s: %(levelname)-8s %(message)s",
121121
)
122-
logging.getLogger(__name__).info(
123-
f"MDAnalysis {version} STARTED logging to {stream!r}"
122+
logging.getLogger("MDAnalysis").info(
123+
f"MDAnalysis {version} STARTED logging to {stream}"
124124
)
125125

126126

@@ -174,6 +174,7 @@ def create(
174174

175175
# replaced with logging.getLogger(__name__)
176176
logger = logging.getLogger(logger_name)
177+
logger.setLevel(logging.DEBUG)
177178

178179
# This checks for file-like object per duck typing
179180
# https://docs.python.org/3/library/logging.handlers.html#streamhandler

testsuite/MDAnalysisTests/lib/test_log.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,22 @@
2424
import pytest
2525
import sys
2626
import logging
27+
import time
2728
import MDAnalysis as mda
2829

2930
from os.path import basename
3031
from MDAnalysis.lib.log import ProgressBar
3132

3233

34+
@pytest.fixture
35+
def fixed_log_time(monkeypatch):
36+
"""Pytest fixture which temporarily set the logging's t0 to the UNIX epoc"""
37+
epoch = 0
38+
39+
monkeypatch.setattr(logging.time, "time", lambda: epoch)
40+
monkeypatch.setattr(logging.Formatter, "converter", time.gmtime)
41+
42+
3343
class TestConvenienceFunctions:
3444
def test_start_logging(self, tmp_path):
3545
mda.start_logging(tmp_path / "MDAnalysis.log")
@@ -57,11 +67,26 @@ def test_stop_logging(self, tmp_path):
5767

5868
assert len(logger.handlers) == 0
5969

60-
def test_message_console(tmp_path):
61-
pass
70+
def test_message_file(self, tmp_path, fixed_log_time):
71+
72+
mda.start_logging(tmp_path / "MDAnalysis.log")
73+
74+
with open(tmp_path / "MDAnalysis.log") as f:
75+
76+
assert (
77+
"1970-01-01 00:00:00,000 MDAnalysis INFO "
78+
+ f"MDAnalysis {mda.version.__version__} STARTED logging to {str(tmp_path)}"
79+
)
80+
81+
def test_message_console(self, tmp_path, capsys):
82+
mda.start_logging(tmp_path / "MDAnalysis.log")
83+
84+
assert (
85+
"MDAnalysis : INFO "
86+
+ f"MDAnalysis {mda.version.__version__} STARTED logging to {str(tmp_path)}"
87+
in capsys.readouterr().out
88+
)
6289

63-
def test_message_file(tmp_path):
64-
pass
6590

6691
# TODO need to make a fixture that can clear all handlers per test
6792
class TestCreateBehaviors:
@@ -93,15 +118,19 @@ def test_exception(tmp_path):
93118
):
94119
mda.lib.log.create(stream=2)
95120

121+
96122
def test_level_parameter():
97123
pass
98124

125+
99126
def test_fmt_parameter():
100127
pass
101128

129+
102130
def test_mode_parameter():
103131
pass
104132

133+
105134
class TestProgressBar(object):
106135

107136
def test_output(self, capsys):

0 commit comments

Comments
 (0)