Skip to content

Instantly share code, notes, and snippets.

@benjiyamin
Created October 24, 2016 17:12
Show Gist options
  • Save benjiyamin/e87a77c5d4d29e76b20c4176d96499f5 to your computer and use it in GitHub Desktop.
Save benjiyamin/e87a77c5d4d29e76b20c4176d96499f5 to your computer and use it in GitHub Desktop.
A PEP-8 unit test for Python's built-in unittest module. Must install the pep8 package ('pip install pep8').
import os
import unittest
import pep8
class Pep8Test(unittest.TestCase):
def test_pep8(self):
style = pep8.StyleGuide()
style.options.max_line_length = 100 # Set this to desired maximum line length
filenames = []
for root, _, files in os.walk('[folder path]'): # Set this to desired folder location
python_files = [f for f in files if f.endswith('.py')]
for file in python_files:
filename = '{0}/{1}'.format(root, file)
filenames.append(filename)
check = style.check_files(filenames)
self.assertEqual(check.total_errors, 0, 'PEP8 style errors: %d' % check.total_errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment