Skip to content

Instantly share code, notes, and snippets.

@rshk
Created March 10, 2016 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rshk/c4240b7ba6080ec00363 to your computer and use it in GitHub Desktop.
Save rshk/c4240b7ba6080ec00363 to your computer and use it in GitHub Desktop.
% py.test -vvv test_py2_logging.py
================================================================================================================================================== test session starts ===================================================================================================================================================
platform linux2 -- Python 2.7.11, pytest-2.9.0, py-1.4.31, pluggy-0.3.1 -- /tmp/tmp.x9LSkBqfG8/.venv2.7/bin/python2.7
cachedir: .cache
rootdir: /tmp/tmp.x9LSkBqfG8, inifile:
collected 4 items
test_py2_logging.py::test_logging_bytes_to_bytes PASSED
test_py2_logging.py::test_logging_unicode_to_bytes PASSED
test_py2_logging.py::test_logging_bytes_to_unicode PASSED
test_py2_logging.py::test_logging_unicode_to_unicode PASSED
================================================================================================================================================ 4 passed in 0.01 seconds ================================================================================================================================================
samu@TrentoRaiser /tmp/tmp.x9LSkBqfG8 py2.7:.venv2.7
% py.test -vvv test_py2_logging.py -s
================================================================================================================================================== test session starts ===================================================================================================================================================
platform linux2 -- Python 2.7.11, pytest-2.9.0, py-1.4.31, pluggy-0.3.1 -- /tmp/tmp.x9LSkBqfG8/.venv2.7/bin/python2.7
cachedir: .cache
rootdir: /tmp/tmp.x9LSkBqfG8, inifile:
collected 4 items
test_py2_logging.py::test_logging_bytes_to_bytes INFO: Hello, ☘!
PASSED
test_py2_logging.py::test_logging_unicode_to_bytes INFO: Hello, ☘!
PASSED
test_py2_logging.py::test_logging_bytes_to_unicode Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/__init__.py", line 853, in emit
msg = self.format(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 726, in format
return fmt.format(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 468, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 7: ordinal not in range(128)
Logged from file test_py2_logging.py, line 54
PASSED
test_py2_logging.py::test_logging_unicode_to_unicode INFO: Hello, ☘!
PASSED
% cat test_py2_logging.py
# -*- coding: utf-8 -*-
import logging
import pytest
import sys
UNICODE_FMT = u'%(levelname)s: %(message)s'
BYTES_FMT = UNICODE_FMT.encode('utf8')
UNICODE_MESSAGE = u'Hello, \u2618!'
BYTES_MESSAGE = UNICODE_MESSAGE.encode('utf8')
@pytest.fixture
def logger():
logger = logging.getLogger('test')
logger.setLevel(logging.INFO)
return logger
@pytest.yield_fixture
def setup_logging_bytes(logger):
formatter = logging.Formatter(BYTES_FMT)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
logger.addHandler(handler)
yield
logger.removeHandler(handler)
@pytest.yield_fixture
def setup_logging_unicode(logger):
formatter = logging.Formatter(UNICODE_FMT)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
logger.addHandler(handler)
yield
logger.removeHandler(handler)
@pytest.mark.usefixtures('setup_logging_bytes')
def test_logging_bytes_to_bytes(logger):
logger.info(BYTES_MESSAGE)
@pytest.mark.usefixtures('setup_logging_bytes')
def test_logging_unicode_to_bytes(logger):
logger.info(UNICODE_MESSAGE)
@pytest.mark.usefixtures('setup_logging_unicode')
def test_logging_bytes_to_unicode(logger):
logger.info(BYTES_MESSAGE)
@pytest.mark.usefixtures('setup_logging_unicode')
def test_logging_unicode_to_unicode(logger):
logger.info(UNICODE_MESSAGE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment