Anonymous avatar Anonymous committed f29a3e1

Initial commit of a py.test plugin to capture log messages.

Comments (0)

Files changed (5)

+syntax: glob
+
+*~
+*.pyo
+*.pyc
+*egg-info
+build
+dist
+env*
+The MIT License
+
+Copyright (c) 2010 Meme Dough
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+pytest-capturelog
+=================
+
+py.test plugin to capture log messages
+
+Installation with pip::
+
+    pip install pytest-capturelog
+
+Installation with easy install::
+
+    easy_install pytest-capturelog

pytest_capturelog.py

+"""py.test plugin to capture log messages"""
+
+import py
+import logging
+
+def pytest_addoption(parser):
+    group = parser.getgroup('general')
+    group.addoption('--nocapturelog',
+                    dest='capturelog',
+                    action='store_false',
+                    default=True,
+                    help='disable log capture')
+
+
+def pytest_runtest_setup(item):
+    if item.config.getvalue('capturelog'):
+        item.capturelog_stream = py.io.TextIO()
+        item.capturelog_handler = logging.StreamHandler(item.capturelog_stream)
+        item.capturelog_formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')
+        item.capturelog_handler.setFormatter(item.capturelog_formatter)
+        root_logger = logging.getLogger()
+        root_logger.addHandler(item.capturelog_handler)
+        root_logger.setLevel(logging.NOTSET)
+
+
+def pytest_runtest_teardown(item):
+    if item.config.getvalue('capturelog'):
+        logging.getLogger().removeHandler(item.capturelog_handler)
+
+
+def pytest_runtest_makereport(__multicall__, item, call):
+    report = __multicall__.execute()
+
+    if item.config.getvalue('capturelog'):
+        if not report.passed:
+            longrepr = getattr(report, 'longrepr', None)
+            if hasattr(longrepr, 'addsection'):
+                log = item.capturelog_stream.getvalue()
+                if log:
+                    longrepr.addsection('Captured log', log)
+
+    return report
+import setuptools
+
+setuptools.setup(name='pytest-capturelog',
+                 version='0.1',
+                 description='py.test plugin to capture log messages',
+                 long_description=open('README').read().strip(),
+                 author='Meme Dough',
+                 author_email='memedough@gmail.com',
+                 url='http://bitbucket.org/memedough/pytest-capturelog/overview',
+                 py_modules=['pytest_capturelog'],
+                 install_requires=['py>=1.1.1'],
+                 entry_points={'pytest11': ['pytest_capturelog = pytest_capturelog']},
+                 license='MIT License',
+                 zip_safe=False,
+                 keywords='py.test pytest',
+                 classifiers=['Development Status :: 4 - Beta',
+                              'Intended Audience :: Developers',
+                              'License :: OSI Approved :: MIT License',
+                              'Operating System :: POSIX :: Linux',
+                              'Programming Language :: Python',
+                              'Topic :: Software Development :: Testing'])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.