Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tomoh1r
Created June 17, 2012 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoh1r/2944993 to your computer and use it in GitHub Desktop.
Save tomoh1r/2944993 to your computer and use it in GitHub Desktop.
flake8 をテストで利用する場合のサンプルを作ってみた。あってんのかなー
# -*- coding: utf-8 -*-
'''以下のようなディレクトリ構成を想定する。
├── src
│    ├── fizzbuzz
│       ├── __init__.py
│        └── main.py
└── test
    ├── __init__.py
   ├── test_flake8.py - このテストファイル
  └── test_main.py - main.py 用テスト
こんな感じに実行する::
$ py.test -v test/test_flake8.py
'''
from os.path import abspath, dirname, join
from glob import glob
import pytest
from flake8.run import check_file
from flake8 import pep8
from flake8 import pyflakes
# プロジェクトディレクトリ
PRJDIR = dirname(dirname(abspath(__file__)))
src_files = glob(join(PRJDIR, 'src', 'fizzbuzz', '*.py'))
test_files = glob(join(PRJDIR, 'test', '*.py'))
class TestFlake8(object):
complexity = -1
@classmethod
def setup_class(cls):
options, args = pep8.process_options()
cls.complexity = options.max_complexity
builtins = set(options.builtins)
if builtins:
orig_builtins = set(pyflakes._MAGIC_GLOBALS)
pyflakes._MAGIC_GLOBALS = orig_builtins | builtins
@pytest.mark.parametrize("filepath", src_files)
def test_src_flake8(self, filepath):
assert check_file(filepath, self.complexity) == 0
@pytest.mark.parametrize("filepath", test_files)
def test_tests_flake8(self, filepath):
assert check_file(filepath, self.complexity) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment