Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a plugin for pylint and pyflakes for nose tests?

I wonder if there is a nose plugin for pylint and/or pyflakes?

Currently I am using coverage and tissue (PEP8) plugins for nose tests.

Tnx in advance

like image 865
brodul Avatar asked Nov 12 '22 23:11

brodul


1 Answers

I once wrote a test generator that uses Pyflakes. It's not a Nose plugin, but it was close enough for my need:

import os
import _ast

from pyflakes import checker

import your_application

TOP = os.path.dirname(os.path.dirname(your_application.__file__))

class PyflakesError(AssertionError):
    def __str__(self):
        path = self.args[0]
        messages = self.args[1]
        messages.sort(key=lambda m: m.lineno)
        return 'checking %s\n' % path + '\n'.join(map(str, messages))

def check(path):
    code = open(os.path.join(TOP, path)).read()
    tree = compile(code, path, "exec", _ast.PyCF_ONLY_AST)
    w = checker.Checker(tree, path)
    if w.messages:
        raise PyflakesError(path, w.messages)

def test():
    for root, dirs, files in os.walk(TOP):
        for name in files:
            if not name.endswith('.py'):
                continue
            yield check, os.path.relpath(os.path.join(root, name), TOP)

        def is_package(d):
            return os.path.exists(os.path.join(root, d, '__init__.py'))
        dirs[:] = filter(is_package, dirs)

The test function yields test cases for each Python file inside the directory containing your_application. You can adjust TOP as needed to test other directories.

like image 143
Martin Geisler Avatar answered Dec 04 '22 04:12

Martin Geisler