aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/util
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-02-22 17:31:10 +0800
committerSerena Feng <feng.xiaowei@zte.com.cn>2017-02-22 15:39:32 +0000
commit050b9f4e7bcb6fc8a91cbadfe56f2387a8343ac0 (patch)
treef311d2dc0d5caaae42285802728c4d8d648fbdff /tests/unit/util
parent955042bf727d85f9a2e215a259c5fc14669e9a9d (diff)
add logger's unittest
Change-Id: I9a7d3c6afeff5432017f5bfd4a961a719c93c086 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'tests/unit/util')
-rw-r--r--tests/unit/util/__init__.py0
-rw-r--r--tests/unit/util/logger_test.py48
2 files changed, 48 insertions, 0 deletions
diff --git a/tests/unit/util/__init__.py b/tests/unit/util/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/unit/util/__init__.py
diff --git a/tests/unit/util/logger_test.py b/tests/unit/util/logger_test.py
new file mode 100644
index 00000000..339b2bf6
--- /dev/null
+++ b/tests/unit/util/logger_test.py
@@ -0,0 +1,48 @@
+import pytest
+
+from qtip.util import logger
+
+MODULE = 'test_logger'
+ERROR_MSG = 'error level test'
+INFO_MSG = 'info level test'
+DEBUG_MSG = 'debug level test'
+
+
+@pytest.fixture()
+def env_home(monkeypatch, tmpdir):
+ monkeypatch.setenv('HOME', str(tmpdir))
+ return tmpdir
+
+
+@pytest.fixture()
+def logger_file(env_home):
+ return env_home.mkdir('qtip').mkdir('logs').join('{}.log'.format(MODULE))
+
+
+def console_expect_debug(content):
+ assert DEBUG_MSG in content
+
+
+def console_expect_nodebug(content):
+ assert DEBUG_MSG not in content
+
+
+@pytest.mark.parametrize('debug, console_expected', [
+ ('true', console_expect_debug),
+ ('false', console_expect_nodebug)])
+def test_logger(monkeypatch, capsys, logger_file, debug, console_expected):
+ monkeypatch.setenv('IF_DEBUG', debug)
+
+ log = logger.QtipLogger(MODULE).get
+ log.error(ERROR_MSG)
+ log.info(INFO_MSG)
+ log.debug(DEBUG_MSG)
+
+ file_print = logger_file.read()
+ assert ERROR_MSG in file_print
+ assert INFO_MSG in file_print
+ assert DEBUG_MSG in file_print
+
+ _, console_print = capsys.readouterr()
+
+ console_expected(console_print)