diff options
author | adim <devnull@localhost> | 2007-03-06 23:44:14 +0100 |
---|---|---|
committer | adim <devnull@localhost> | 2007-03-06 23:44:14 +0100 |
commit | 13d6dfda3d7c3f84b3d16362905aa6375eb21a3a (patch) | |
tree | 04a2e4a837a58b51e559173ad8bc88fa490deb58 /bin | |
parent | 73903a79a8834d826b9c236f3861d93dbe7bf9fd (diff) | |
download | logilab-common-13d6dfda3d7c3f84b3d16362905aa6375eb21a3a.tar.gz |
made pytest able to test a single file.
One advantage is that the testfile being tested does not need
to be based on logilab.common.testlib. pytest will happily monkeypatch
unittest (and doctest) to use lgc.testlib classes instead of standard ones.
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/pytest | 52 |
1 files changed, 43 insertions, 9 deletions
@@ -1,8 +1,19 @@ -#!/usr/bin/python +#!/usr/bin/env python import os, sys import os.path as osp from logilab.common import testlib +import doctest +import unittest + +# monkeypatch unittest and doctest (ouch !) +unittest.TestCase = testlib.TestCase +unittest.main = testlib.unittest_main +unittest._TextTestResult = testlib.SkipAwareTestResult +unittest.TextTestRunner = testlib.SkipAwareTextTestRunner +unittest.TestLoader = testlib.NonStrictTestLoader +unittest.TestProgram = testlib.SkipAwareTestProgram +doctest.DocTestCase.__bases__ = (testlib.TestCase,) def autopath(projdir=os.getcwd()): @@ -16,12 +27,35 @@ def autopath(projdir=os.getcwd()): else: sys.path.insert(0, curdir) -autopath() +def testfile(filename): + sys.argv.remove(filename) + here = os.getcwd() + dirname = osp.dirname(filename) + if dirname: + os.chdir(dirname) + sys.path.insert(0, '') + modname = osp.basename(filename)[:-3] + testlib.unittest_main(modname) + +def testall(): + errcode = 0 + for dirname, dirs, files in os.walk(os.getcwd()): + for skipped in ('CVS', '.svn', '.hg'): + if skipped in dirs: + dirs.remove(skipped) + basename = osp.basename(dirname) + if basename in ('test', 'tests'): + errcode += testlib.main(dirname, exitafter=False) + return errcode + +if __name__ == '__main__': + autopath() + filenames = [arg for arg in sys.argv[1:] if arg.endswith('.py')] + if filenames: + if len(filenames) > 1: + print "Usage: pytest [filename]" + sys.exit(-1) + # testfile will exit directly + testfile(filenames[0]) + sys.exit(testall()) -for dirname, dirs, files in os.walk(os.getcwd()): - for skipped in ('CVS', '.svn', '.hg'): - if skipped in dirs: - dirs.remove(skipped) - basename = osp.basename(dirname) - if basename in ('test', 'tests'): - testlib.main(dirname, exitafter=False) |