Skip to content

Instantly share code, notes, and snippets.

@Paulius-Maruska
Last active April 19, 2018 08:29
Show Gist options
  • Save Paulius-Maruska/055eede98347ee306939d43c35cfe459 to your computer and use it in GitHub Desktop.
Save Paulius-Maruska/055eede98347ee306939d43c35cfe459 to your computer and use it in GitHub Desktop.
pytest regression with logging and doctests
import logging
def myfunc(a, b, c, d, logger=None):
"""My func is super awesome.
Example:
>>> import logging, sys
>>> logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(levelname)s: %(message)s")
>>> myfunc(1, 2, 3, 4)
DEBUG: a=1, b=2, c=3, d=4, result=10
10
"""
if logger is None:
logger = logging.getLogger()
result = a + b + c + d
logger.debug("a=%r, b=%r, c=%r, d=%r, result=%r", a, b, c, d, result)
return result
=========================================================================
Running with doctest
=========================================================================
Running with unittest
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
=========================================================================
Running with pytest
============================= test session starts ==============================
platform linux -- Python 3.6.5, pytest-3.2.5, py-1.5.3, pluggy-0.4.0
rootdir: /home/paulius/Projects/experiments/doctests, inifile:
collected 2 items
mypackage.py .
test_mypackage.py .
=========================== 2 passed in 0.02 seconds ===========================
=========================================================================
Running with doctest
=========================================================================
Running with unittest
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
=========================================================================
Running with pytest
============================= test session starts ==============================
platform linux -- Python 3.6.5, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/paulius/Projects/experiments/doctests, inifile:
collected 2 items
mypackage.py F [ 50%]
test_mypackage.py . [100%]
=================================== FAILURES ===================================
__________________________ [doctest] mypackage.myfunc __________________________
005 My func is super awesome.
006
007 Example:
008 >>> import logging, sys
009 >>> logging.basicConfig(
010 ... stream=sys.stdout,
011 ... level=logging.DEBUG,
012 ... format="%(levelname)s: %(message)s")
013 >>> myfunc(1, 2, 3, 4)
Expected:
DEBUG: a=1, b=2, c=3, d=4, result=10
10
Got:
10
/home/paulius/Projects/experiments/doctests/mypackage.py:13: DocTestFailure
====================== 1 failed, 1 passed in 0.01 seconds ======================
#!/usr/bin/env bash
# ./run.sh > output-pytest-<pytest version>.txt 2>&1
echo "========================================================================="
echo " Running with doctest"
python -m doctest *.py
echo "========================================================================="
echo " Running with unittest"
python -m unittest *.py
echo "========================================================================="
echo " Running with pytest"
python -m pytest --doctest-modules *.py
import doctest
import unittest
import mypackage
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(mypackage))
return tests
class MyPackageTests(unittest.TestCase):
def test_myfunc_returns_sum(self):
self.assertEqual(mypackage.myfunc(1, 2, 3, 4), 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment