summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2013-03-01 18:16:47 +0100
committerFlorent Xicluna <florent.xicluna@gmail.com>2013-03-01 18:16:47 +0100
commitf9609fb02cc09926cbc849053840b9c4f61694f2 (patch)
tree81e2673a17614b4b4b641d9b32b89159291fa1a7
parent920c1432b2f1b0cd4a999ecba84cae07513f6fc5 (diff)
downloadpep8-f9609fb02cc09926cbc849053840b9c4f61694f2.tar.gz
Reorganize the test suite using unittest.
-rwxr-xr-xpep8.py4
-rw-r--r--testsuite/__init__.py0
-rw-r--r--testsuite/support.py (renamed from testsuite/test_pep8.py)102
-rw-r--r--testsuite/test_all.py53
4 files changed, 100 insertions, 59 deletions
diff --git a/pep8.py b/pep8.py
index 6dc4fb8..67ba07e 100755
--- a/pep8.py
+++ b/pep8.py
@@ -1844,9 +1844,7 @@ def _main():
pep8style = StyleGuide(parse_argv=True, config_file=True)
options = pep8style.options
if options.doctest or options.testsuite:
- sys.path[:0] = [TESTSUITE_PATH]
- from test_pep8 import run_tests
- del sys.path[0]
+ from testsuite.support import run_tests
report = run_tests(pep8style, options.doctest, options.testsuite)
else:
report = pep8style.check_files()
diff --git a/testsuite/__init__.py b/testsuite/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testsuite/__init__.py
diff --git a/testsuite/test_pep8.py b/testsuite/support.py
index 1ad559f..bcdcc19 100644
--- a/testsuite/test_pep8.py
+++ b/testsuite/support.py
@@ -1,11 +1,8 @@
-#!/usr/bin/env python
# -*- coding: utf-8 -*-
-import os.path
import re
import sys
-import pep8
-from pep8 import Checker, StyleGuide, BaseReport, StandardReport, readlines
+from pep8 import Checker, BaseReport, StandardReport, readlines
SELFTEST_REGEX = re.compile(r'\b(Okay|[EW]\d{3}):\s(.*)')
@@ -49,6 +46,49 @@ class TestReport(StandardReport):
print("Test failed." if self.total_errors else "Test passed.")
+def selftest(options):
+ """
+ Test all check functions with test cases in docstrings.
+ """
+ count_failed = count_all = 0
+ report = BaseReport(options)
+ counters = report.counters
+ checks = options.physical_checks + options.logical_checks
+ for name, check, argument_names in checks:
+ for line in check.__doc__.splitlines():
+ line = line.lstrip()
+ match = SELFTEST_REGEX.match(line)
+ if match is None:
+ continue
+ code, source = match.groups()
+ lines = [part.replace(r'\t', '\t') + '\n'
+ for part in source.split(r'\n')]
+ checker = Checker(lines=lines, options=options, report=report)
+ checker.check_all()
+ error = None
+ if code == 'Okay':
+ if len(counters) > len(options.benchmark_keys):
+ codes = [key for key in counters
+ if key not in options.benchmark_keys]
+ error = "incorrectly found %s" % ', '.join(codes)
+ elif not counters.get(code):
+ error = "failed to find %s" % code
+ # Keep showing errors for multiple tests
+ for key in set(counters) - set(options.benchmark_keys):
+ del counters[key]
+ report.messages = {}
+ count_all += 1
+ if not error:
+ if options.verbose:
+ print("%s: %s" % (code, source))
+ else:
+ count_failed += 1
+ print("pep8.py: %s:" % error)
+ for line in checker.lines:
+ print(line.rstrip())
+ return count_failed, count_all
+
+
def init_tests(pep8style):
"""
Initialize testing framework.
@@ -98,49 +138,7 @@ def init_tests(pep8style):
return report.counters['failed tests']
pep8style.runner = run_tests
-
-
-def selftest(options):
- """
- Test all check functions with test cases in docstrings.
- """
- count_failed = count_all = 0
- report = BaseReport(options)
- counters = report.counters
- checks = options.physical_checks + options.logical_checks
- for name, check, argument_names in checks:
- for line in check.__doc__.splitlines():
- line = line.lstrip()
- match = SELFTEST_REGEX.match(line)
- if match is None:
- continue
- code, source = match.groups()
- lines = [part.replace(r'\t', '\t') + '\n'
- for part in source.split(r'\n')]
- checker = Checker(lines=lines, options=options, report=report)
- checker.check_all()
- error = None
- if code == 'Okay':
- if len(counters) > len(options.benchmark_keys):
- codes = [key for key in counters
- if key not in options.benchmark_keys]
- error = "incorrectly found %s" % ', '.join(codes)
- elif not counters.get(code):
- error = "failed to find %s" % code
- # Keep showing errors for multiple tests
- for key in set(counters) - set(options.benchmark_keys):
- del counters[key]
- report.messages = {}
- count_all += 1
- if not error:
- if options.verbose:
- print("%s: %s" % (code, source))
- else:
- count_failed += 1
- print("%s: %s:" % (pep8.__file__, error))
- for line in checker.lines:
- print(line.rstrip())
- return count_failed, count_all
+init_tests.__test__ = False
def run_tests(style, doctest=True, testsuite=False):
@@ -159,12 +157,4 @@ def run_tests(style, doctest=True, testsuite=False):
if testsuite:
init_tests(style)
return style.check_files()
-
-
-if __name__ == '__main__':
- if len(sys.argv) > 1:
- sys.exit(pep8._main())
- pep8style = StyleGuide(paths=[os.path.dirname(__file__)], ignore=None)
- report = run_tests(pep8style, doctest=True, testsuite=True)
- report.print_results()
- sys.exit(1 if report.total_errors else 0)
+run_tests.__test__ = False
diff --git a/testsuite/test_all.py b/testsuite/test_all.py
new file mode 100644
index 0000000..7d18f8c
--- /dev/null
+++ b/testsuite/test_all.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import os.path
+import sys
+import unittest
+
+import pep8
+from testsuite.support import init_tests, selftest
+
+ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
+
+
+class Pep8TestCase(unittest.TestCase):
+
+ def setUp(self):
+ self._style = pep8.StyleGuide(
+ paths=[os.path.dirname(__file__)],
+ ignore=None, quiet=True)
+
+ def test_doctest(self):
+ import doctest
+ fail_d, done_d = doctest.testmod(pep8, verbose=False, report=False)
+ self.assertTrue(done_d, msg='tests not found')
+ self.assertFalse(fail_d, msg='%s failure(s)' % fail_d)
+
+ def test_selftest(self):
+ fail_s, done_s = selftest(self._style.options)
+ self.assertTrue(done_s, msg='tests not found')
+ self.assertFalse(fail_s, msg='%s failure(s)' % fail_s)
+
+ def test_checkers_testsuite(self):
+ init_tests(self._style)
+ report = self._style.check_files()
+ self.assertFalse(report.total_errors,
+ msg='%s failure(s)' % report.total_errors)
+
+ def test_own_dog_food(self):
+ files = [pep8.__file__.rstrip('oc'), __file__.rstrip('oc'),
+ os.path.join(ROOT_DIR, 'setup.py')]
+ report = self._style.init_report(pep8.StandardReport)
+ report = self._style.check_files(files)
+ self.assertFalse(report.total_errors,
+ msg='Failures: %s' % report.messages)
+
+
+def _main():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(Pep8TestCase))
+ runner = unittest.TextTestRunner(verbosity=2)
+ return runner.run(suite)
+
+if __name__ == '__main__':
+ sys.exit(not _main())