pytest.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This file is part of logilab-common.
  5. #
  6. # logilab-common is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU Lesser General Public License as published by the Free
  8. # Software Foundation, either version 2.1 of the License, or (at your option) any
  9. # later version.
  10. #
  11. # logilab-common is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License along
  17. # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
  18. """pytest is a tool that eases test running and debugging.
  19. To be able to use pytest, you should either write tests using
  20. the logilab.common.testlib's framework or the unittest module of the
  21. Python's standard library.
  22. You can customize pytest's behaviour by defining a ``pytestconf.py`` file
  23. somewhere in your test directory. In this file, you can add options or
  24. change the way tests are run.
  25. To add command line options, you must define a ``update_parser`` function in
  26. your ``pytestconf.py`` file. The function must accept a single parameter
  27. that will be the OptionParser's instance to customize.
  28. If you wish to customize the tester, you'll have to define a class named
  29. ``CustomPyTester``. This class should extend the default `PyTester` class
  30. defined in the pytest module. Take a look at the `PyTester` and `DjangoTester`
  31. classes for more information about what can be done.
  32. For instance, if you wish to add a custom -l option to specify a loglevel, you
  33. could define the following ``pytestconf.py`` file ::
  34. import logging
  35. from logilab.common.pytest import PyTester
  36. def update_parser(parser):
  37. parser.add_option('-l', '--loglevel', dest='loglevel', action='store',
  38. choices=('debug', 'info', 'warning', 'error', 'critical'),
  39. default='critical', help="the default log level possible choices are "
  40. "('debug', 'info', 'warning', 'error', 'critical')")
  41. return parser
  42. class CustomPyTester(PyTester):
  43. def __init__(self, cvg, options):
  44. super(CustomPyTester, self).__init__(cvg, options)
  45. loglevel = options.loglevel.upper()
  46. logger = logging.getLogger('erudi')
  47. logger.setLevel(logging.getLevelName(loglevel))
  48. In your TestCase class you can then get the value of a specific option with
  49. the ``optval`` method::
  50. class MyTestCase(TestCase):
  51. def test_foo(self):
  52. loglevel = self.optval('loglevel')
  53. # ...
  54. You can also tag your tag your test for fine filtering
  55. With those tag::
  56. from logilab.common.testlib import tag, TestCase
  57. class Exemple(TestCase):
  58. @tag('rouge', 'carre')
  59. def toto(self):
  60. pass
  61. @tag('carre', 'vert')
  62. def tata(self):
  63. pass
  64. @tag('rouge')
  65. def titi(test):
  66. pass
  67. you can filter the function with a simple python expression
  68. * ``toto`` and ``titi`` match ``rouge``
  69. * ``toto``, ``tata`` and ``titi``, match ``rouge or carre``
  70. * ``tata`` and ``titi`` match``rouge ^ carre``
  71. * ``titi`` match ``rouge and not carre``
  72. """
  73. __docformat__ = "restructuredtext en"
  74. PYTEST_DOC = """%prog [OPTIONS] [testfile [testpattern]]
  75. examples:
  76. pytest path/to/mytests.py
  77. pytest path/to/mytests.py TheseTests
  78. pytest path/to/mytests.py TheseTests.test_thisone
  79. pytest path/to/mytests.py -m '(not long and database) or regr'
  80. pytest one (will run both test_thisone and test_thatone)
  81. pytest path/to/mytests.py -s not (will skip test_notthisone)
  82. pytest --coverage test_foo.py
  83. (only if logilab.devtools is available)
  84. """
  85. ENABLE_DBC = False
  86. FILE_RESTART = ".pytest.restart"
  87. import os, sys, re
  88. import os.path as osp
  89. from time import time, clock
  90. import warnings
  91. import types
  92. from logilab.common.fileutils import abspath_listdir
  93. from logilab.common import textutils
  94. from logilab.common import testlib, STD_BLACKLIST
  95. # use the same unittest module as testlib
  96. from logilab.common.testlib import unittest, start_interactive_mode
  97. from logilab.common.compat import any
  98. import doctest
  99. import unittest as unittest_legacy
  100. if not getattr(unittest_legacy, "__package__", None):
  101. try:
  102. import unittest2.suite as unittest_suite
  103. except ImportError:
  104. sys.exit("You have to install python-unittest2 to use this module")
  105. else:
  106. import unittest.suite as unittest_suite
  107. try:
  108. import django
  109. from logilab.common.modutils import modpath_from_file, load_module_from_modpath
  110. DJANGO_FOUND = True
  111. except ImportError:
  112. DJANGO_FOUND = False
  113. CONF_FILE = 'pytestconf.py'
  114. ## coverage hacks, do not read this, do not read this, do not read this
  115. # hey, but this is an aspect, right ?!!!
  116. class TraceController(object):
  117. nesting = 0
  118. def pause_tracing(cls):
  119. if not cls.nesting:
  120. cls.tracefunc = staticmethod(getattr(sys, '__settrace__', sys.settrace))
  121. cls.oldtracer = getattr(sys, '__tracer__', None)
  122. sys.__notrace__ = True
  123. cls.tracefunc(None)
  124. cls.nesting += 1
  125. pause_tracing = classmethod(pause_tracing)
  126. def resume_tracing(cls):
  127. cls.nesting -= 1
  128. assert cls.nesting >= 0
  129. if not cls.nesting:
  130. cls.tracefunc(cls.oldtracer)
  131. delattr(sys, '__notrace__')
  132. resume_tracing = classmethod(resume_tracing)
  133. pause_tracing = TraceController.pause_tracing
  134. resume_tracing = TraceController.resume_tracing
  135. def nocoverage(func):
  136. if hasattr(func, 'uncovered'):
  137. return func
  138. func.uncovered = True
  139. def not_covered(*args, **kwargs):
  140. pause_tracing()
  141. try:
  142. return func(*args, **kwargs)
  143. finally:
  144. resume_tracing()
  145. not_covered.uncovered = True
  146. return not_covered
  147. ## end of coverage hacks
  148. TESTFILE_RE = re.compile("^((unit)?test.*|smoketest)\.py$")
  149. def this_is_a_testfile(filename):
  150. """returns True if `filename` seems to be a test file"""
  151. return TESTFILE_RE.match(osp.basename(filename))
  152. TESTDIR_RE = re.compile("^(unit)?tests?$")
  153. def this_is_a_testdir(dirpath):
  154. """returns True if `filename` seems to be a test directory"""
  155. return TESTDIR_RE.match(osp.basename(dirpath))
  156. def load_pytest_conf(path, parser):
  157. """loads a ``pytestconf.py`` file and update default parser
  158. and / or tester.
  159. """
  160. namespace = {}
  161. execfile(path, namespace)
  162. if 'update_parser' in namespace:
  163. namespace['update_parser'](parser)
  164. return namespace.get('CustomPyTester', PyTester)
  165. def project_root(parser, projdir=os.getcwd()):
  166. """try to find project's root and add it to sys.path"""
  167. previousdir = curdir = osp.abspath(projdir)
  168. testercls = PyTester
  169. conf_file_path = osp.join(curdir, CONF_FILE)
  170. if osp.isfile(conf_file_path):
  171. testercls = load_pytest_conf(conf_file_path, parser)
  172. while this_is_a_testdir(curdir) or \
  173. osp.isfile(osp.join(curdir, '__init__.py')):
  174. newdir = osp.normpath(osp.join(curdir, os.pardir))
  175. if newdir == curdir:
  176. break
  177. previousdir = curdir
  178. curdir = newdir
  179. conf_file_path = osp.join(curdir, CONF_FILE)
  180. if osp.isfile(conf_file_path):
  181. testercls = load_pytest_conf(conf_file_path, parser)
  182. return previousdir, testercls
  183. class GlobalTestReport(object):
  184. """this class holds global test statistics"""
  185. def __init__(self):
  186. self.ran = 0
  187. self.skipped = 0
  188. self.failures = 0
  189. self.errors = 0
  190. self.ttime = 0
  191. self.ctime = 0
  192. self.modulescount = 0
  193. self.errmodules = []
  194. def feed(self, filename, testresult, ttime, ctime):
  195. """integrates new test information into internal statistics"""
  196. ran = testresult.testsRun
  197. self.ran += ran
  198. self.skipped += len(getattr(testresult, 'skipped', ()))
  199. self.failures += len(testresult.failures)
  200. self.errors += len(testresult.errors)
  201. self.ttime += ttime
  202. self.ctime += ctime
  203. self.modulescount += 1
  204. if not testresult.wasSuccessful():
  205. problems = len(testresult.failures) + len(testresult.errors)
  206. self.errmodules.append((filename[:-3], problems, ran))
  207. def failed_to_test_module(self, filename):
  208. """called when the test module could not be imported by unittest
  209. """
  210. self.errors += 1
  211. self.modulescount += 1
  212. self.ran += 1
  213. self.errmodules.append((filename[:-3], 1, 1))
  214. def skip_module(self, filename):
  215. self.modulescount += 1
  216. self.ran += 1
  217. self.errmodules.append((filename[:-3], 0, 0))
  218. def __str__(self):
  219. """this is just presentation stuff"""
  220. line1 = ['Ran %s test cases in %.2fs (%.2fs CPU)'
  221. % (self.ran, self.ttime, self.ctime)]
  222. if self.errors:
  223. line1.append('%s errors' % self.errors)
  224. if self.failures:
  225. line1.append('%s failures' % self.failures)
  226. if self.skipped:
  227. line1.append('%s skipped' % self.skipped)
  228. modulesok = self.modulescount - len(self.errmodules)
  229. if self.errors or self.failures:
  230. line2 = '%s modules OK (%s failed)' % (modulesok,
  231. len(self.errmodules))
  232. descr = ', '.join(['%s [%s/%s]' % info for info in self.errmodules])
  233. line3 = '\nfailures: %s' % descr
  234. elif modulesok:
  235. line2 = 'All %s modules OK' % modulesok
  236. line3 = ''
  237. else:
  238. return ''
  239. return '%s\n%s%s' % (', '.join(line1), line2, line3)
  240. def remove_local_modules_from_sys(testdir):
  241. """remove all modules from cache that come from `testdir`
  242. This is used to avoid strange side-effects when using the
  243. testall() mode of pytest.
  244. For instance, if we run pytest on this tree::
  245. A/test/test_utils.py
  246. B/test/test_utils.py
  247. we **have** to clean sys.modules to make sure the correct test_utils
  248. module is ran in B
  249. """
  250. for modname, mod in sys.modules.items():
  251. if mod is None:
  252. continue
  253. if not hasattr(mod, '__file__'):
  254. # this is the case of some built-in modules like sys, imp, marshal
  255. continue
  256. modfile = mod.__file__
  257. # if modfile is not an absolute path, it was probably loaded locally
  258. # during the tests
  259. if not osp.isabs(modfile) or modfile.startswith(testdir):
  260. del sys.modules[modname]
  261. class PyTester(object):
  262. """encapsulates testrun logic"""
  263. def __init__(self, cvg, options):
  264. self.report = GlobalTestReport()
  265. self.cvg = cvg
  266. self.options = options
  267. self.firstwrite = True
  268. self._errcode = None
  269. def show_report(self):
  270. """prints the report and returns appropriate exitcode"""
  271. # everything has been ran, print report
  272. print "*" * 79
  273. print self.report
  274. def get_errcode(self):
  275. # errcode set explicitly
  276. if self._errcode is not None:
  277. return self._errcode
  278. return self.report.failures + self.report.errors
  279. def set_errcode(self, errcode):
  280. self._errcode = errcode
  281. errcode = property(get_errcode, set_errcode)
  282. def testall(self, exitfirst=False):
  283. """walks through current working directory, finds something
  284. which can be considered as a testdir and runs every test there
  285. """
  286. here = os.getcwd()
  287. for dirname, dirs, _ in os.walk(here):
  288. for skipped in STD_BLACKLIST:
  289. if skipped in dirs:
  290. dirs.remove(skipped)
  291. basename = osp.basename(dirname)
  292. if this_is_a_testdir(basename):
  293. print "going into", dirname
  294. # we found a testdir, let's explore it !
  295. if not self.testonedir(dirname, exitfirst):
  296. break
  297. dirs[:] = []
  298. if self.report.ran == 0:
  299. print "no test dir found testing here:", here
  300. # if no test was found during the visit, consider
  301. # the local directory as a test directory even if
  302. # it doesn't have a traditional test directory name
  303. self.testonedir(here)
  304. def testonedir(self, testdir, exitfirst=False):
  305. """finds each testfile in the `testdir` and runs it
  306. return true when all tests has been executed, false if exitfirst and
  307. some test has failed.
  308. """
  309. for filename in abspath_listdir(testdir):
  310. if this_is_a_testfile(filename):
  311. if self.options.exitfirst and not self.options.restart:
  312. # overwrite restart file
  313. try:
  314. restartfile = open(FILE_RESTART, "w")
  315. restartfile.close()
  316. except Exception, e:
  317. print >> sys.__stderr__, "Error while overwriting \
  318. succeeded test file :", osp.join(os.getcwd(), FILE_RESTART)
  319. raise e
  320. # run test and collect information
  321. prog = self.testfile(filename, batchmode=True)
  322. if exitfirst and (prog is None or not prog.result.wasSuccessful()):
  323. return False
  324. self.firstwrite = True
  325. # clean local modules
  326. remove_local_modules_from_sys(testdir)
  327. return True
  328. def testfile(self, filename, batchmode=False):
  329. """runs every test in `filename`
  330. :param filename: an absolute path pointing to a unittest file
  331. """
  332. here = os.getcwd()
  333. dirname = osp.dirname(filename)
  334. if dirname:
  335. os.chdir(dirname)
  336. # overwrite restart file if it has not been done already
  337. if self.options.exitfirst and not self.options.restart and self.firstwrite:
  338. try:
  339. restartfile = open(FILE_RESTART, "w")
  340. restartfile.close()
  341. except Exception, e:
  342. print >> sys.__stderr__, "Error while overwriting \
  343. succeeded test file :", osp.join(os.getcwd(), FILE_RESTART)
  344. raise e
  345. modname = osp.basename(filename)[:-3]
  346. try:
  347. print >> sys.stderr, (' %s ' % osp.basename(filename)).center(70, '=')
  348. except TypeError: # < py 2.4 bw compat
  349. print >> sys.stderr, (' %s ' % osp.basename(filename)).center(70)
  350. try:
  351. tstart, cstart = time(), clock()
  352. try:
  353. testprog = SkipAwareTestProgram(modname, batchmode=batchmode, cvg=self.cvg,
  354. options=self.options, outstream=sys.stderr)
  355. except KeyboardInterrupt:
  356. raise
  357. except SystemExit, exc:
  358. self.errcode = exc.code
  359. raise
  360. except testlib.SkipTest:
  361. print "Module skipped:", filename
  362. self.report.skip_module(filename)
  363. return None
  364. except Exception:
  365. self.report.failed_to_test_module(filename)
  366. print >> sys.stderr, 'unhandled exception occurred while testing', modname
  367. import traceback
  368. traceback.print_exc(file=sys.stderr)
  369. return None
  370. tend, cend = time(), clock()
  371. ttime, ctime = (tend - tstart), (cend - cstart)
  372. self.report.feed(filename, testprog.result, ttime, ctime)
  373. return testprog
  374. finally:
  375. if dirname:
  376. os.chdir(here)
  377. class DjangoTester(PyTester):
  378. def load_django_settings(self, dirname):
  379. """try to find project's setting and load it"""
  380. curdir = osp.abspath(dirname)
  381. previousdir = curdir
  382. while not osp.isfile(osp.join(curdir, 'settings.py')) and \
  383. osp.isfile(osp.join(curdir, '__init__.py')):
  384. newdir = osp.normpath(osp.join(curdir, os.pardir))
  385. if newdir == curdir:
  386. raise AssertionError('could not find settings.py')
  387. previousdir = curdir
  388. curdir = newdir
  389. # late django initialization
  390. settings = load_module_from_modpath(modpath_from_file(osp.join(curdir, 'settings.py')))
  391. from django.core.management import setup_environ
  392. setup_environ(settings)
  393. settings.DEBUG = False
  394. self.settings = settings
  395. # add settings dir to pythonpath since it's the project's root
  396. if curdir not in sys.path:
  397. sys.path.insert(1, curdir)
  398. def before_testfile(self):
  399. # Those imports must be done **after** setup_environ was called
  400. from django.test.utils import setup_test_environment
  401. from django.test.utils import create_test_db
  402. setup_test_environment()
  403. create_test_db(verbosity=0)
  404. self.dbname = self.settings.TEST_DATABASE_NAME
  405. def after_testfile(self):
  406. # Those imports must be done **after** setup_environ was called
  407. from django.test.utils import teardown_test_environment
  408. from django.test.utils import destroy_test_db
  409. teardown_test_environment()
  410. print 'destroying', self.dbname
  411. destroy_test_db(self.dbname, verbosity=0)
  412. def testall(self, exitfirst=False):
  413. """walks through current working directory, finds something
  414. which can be considered as a testdir and runs every test there
  415. """
  416. for dirname, dirs, files in os.walk(os.getcwd()):
  417. for skipped in ('CVS', '.svn', '.hg'):
  418. if skipped in dirs:
  419. dirs.remove(skipped)
  420. if 'tests.py' in files:
  421. if not self.testonedir(dirname, exitfirst):
  422. break
  423. dirs[:] = []
  424. else:
  425. basename = osp.basename(dirname)
  426. if basename in ('test', 'tests'):
  427. print "going into", dirname
  428. # we found a testdir, let's explore it !
  429. if not self.testonedir(dirname, exitfirst):
  430. break
  431. dirs[:] = []
  432. def testonedir(self, testdir, exitfirst=False):
  433. """finds each testfile in the `testdir` and runs it
  434. return true when all tests has been executed, false if exitfirst and
  435. some test has failed.
  436. """
  437. # special django behaviour : if tests are splitted in several files,
  438. # remove the main tests.py file and tests each test file separately
  439. testfiles = [fpath for fpath in abspath_listdir(testdir)
  440. if this_is_a_testfile(fpath)]
  441. if len(testfiles) > 1:
  442. try:
  443. testfiles.remove(osp.join(testdir, 'tests.py'))
  444. except ValueError:
  445. pass
  446. for filename in testfiles:
  447. # run test and collect information
  448. prog = self.testfile(filename, batchmode=True)
  449. if exitfirst and (prog is None or not prog.result.wasSuccessful()):
  450. return False
  451. # clean local modules
  452. remove_local_modules_from_sys(testdir)
  453. return True
  454. def testfile(self, filename, batchmode=False):
  455. """runs every test in `filename`
  456. :param filename: an absolute path pointing to a unittest file
  457. """
  458. here = os.getcwd()
  459. dirname = osp.dirname(filename)
  460. if dirname:
  461. os.chdir(dirname)
  462. self.load_django_settings(dirname)
  463. modname = osp.basename(filename)[:-3]
  464. print >>sys.stderr, (' %s ' % osp.basename(filename)).center(70, '=')
  465. try:
  466. try:
  467. tstart, cstart = time(), clock()
  468. self.before_testfile()
  469. testprog = SkipAwareTestProgram(modname, batchmode=batchmode, cvg=self.cvg)
  470. tend, cend = time(), clock()
  471. ttime, ctime = (tend - tstart), (cend - cstart)
  472. self.report.feed(filename, testprog.result, ttime, ctime)
  473. return testprog
  474. except SystemExit:
  475. raise
  476. except Exception, exc:
  477. import traceback
  478. traceback.print_exc()
  479. self.report.failed_to_test_module(filename)
  480. print 'unhandled exception occurred while testing', modname
  481. print 'error: %s' % exc
  482. return None
  483. finally:
  484. self.after_testfile()
  485. if dirname:
  486. os.chdir(here)
  487. def make_parser():
  488. """creates the OptionParser instance
  489. """
  490. from optparse import OptionParser
  491. parser = OptionParser(usage=PYTEST_DOC)
  492. parser.newargs = []
  493. def rebuild_cmdline(option, opt, value, parser):
  494. """carry the option to unittest_main"""
  495. parser.newargs.append(opt)
  496. def rebuild_and_store(option, opt, value, parser):
  497. """carry the option to unittest_main and store
  498. the value on current parser
  499. """
  500. parser.newargs.append(opt)
  501. setattr(parser.values, option.dest, True)
  502. def capture_and_rebuild(option, opt, value, parser):
  503. warnings.simplefilter('ignore', DeprecationWarning)
  504. rebuild_cmdline(option, opt, value, parser)
  505. # pytest options
  506. parser.add_option('-t', dest='testdir', default=None,
  507. help="directory where the tests will be found")
  508. parser.add_option('-d', dest='dbc', default=False,
  509. action="store_true", help="enable design-by-contract")
  510. # unittest_main options provided and passed through pytest
  511. parser.add_option('-v', '--verbose', callback=rebuild_cmdline,
  512. action="callback", help="Verbose output")
  513. parser.add_option('-i', '--pdb', callback=rebuild_and_store,
  514. dest="pdb", action="callback",
  515. help="Enable test failure inspection (conflicts with --coverage)")
  516. parser.add_option('-x', '--exitfirst', callback=rebuild_and_store,
  517. dest="exitfirst", default=False,
  518. action="callback", help="Exit on first failure "
  519. "(only make sense when pytest run one test file)")
  520. parser.add_option('-R', '--restart', callback=rebuild_and_store,
  521. dest="restart", default=False,
  522. action="callback",
  523. help="Restart tests from where it failed (implies exitfirst) "
  524. "(only make sense if tests previously ran with exitfirst only)")
  525. parser.add_option('--color', callback=rebuild_cmdline,
  526. action="callback",
  527. help="colorize tracebacks")
  528. parser.add_option('-s', '--skip',
  529. # XXX: I wish I could use the callback action but it
  530. # doesn't seem to be able to get the value
  531. # associated to the option
  532. action="store", dest="skipped", default=None,
  533. help="test names matching this name will be skipped "
  534. "to skip several patterns, use commas")
  535. parser.add_option('-q', '--quiet', callback=rebuild_cmdline,
  536. action="callback", help="Minimal output")
  537. parser.add_option('-P', '--profile', default=None, dest='profile',
  538. help="Profile execution and store data in the given file")
  539. parser.add_option('-m', '--match', default=None, dest='tags_pattern',
  540. help="only execute test whose tag match the current pattern")
  541. try:
  542. from logilab.devtools.lib.coverage import Coverage
  543. parser.add_option('--coverage', dest="coverage", default=False,
  544. action="store_true",
  545. help="run tests with pycoverage (conflicts with --pdb)")
  546. except ImportError:
  547. pass
  548. if DJANGO_FOUND:
  549. parser.add_option('-J', '--django', dest='django', default=False,
  550. action="store_true",
  551. help='use pytest for django test cases')
  552. return parser
  553. def parseargs(parser):
  554. """Parse the command line and return (options processed), (options to pass to
  555. unittest_main()), (explicitfile or None).
  556. """
  557. # parse the command line
  558. options, args = parser.parse_args()
  559. if options.pdb and getattr(options, 'coverage', False):
  560. parser.error("'pdb' and 'coverage' options are exclusive")
  561. filenames = [arg for arg in args if arg.endswith('.py')]
  562. if filenames:
  563. if len(filenames) > 1:
  564. parser.error("only one filename is acceptable")
  565. explicitfile = filenames[0]
  566. args.remove(explicitfile)
  567. else:
  568. explicitfile = None
  569. # someone wants DBC
  570. testlib.ENABLE_DBC = options.dbc
  571. newargs = parser.newargs
  572. if options.skipped:
  573. newargs.extend(['--skip', options.skipped])
  574. # restart implies exitfirst
  575. if options.restart:
  576. options.exitfirst = True
  577. # append additional args to the new sys.argv and let unittest_main
  578. # do the rest
  579. newargs += args
  580. return options, explicitfile
  581. def run():
  582. parser = make_parser()
  583. rootdir, testercls = project_root(parser)
  584. options, explicitfile = parseargs(parser)
  585. # mock a new command line
  586. sys.argv[1:] = parser.newargs
  587. covermode = getattr(options, 'coverage', None)
  588. cvg = None
  589. if not '' in sys.path:
  590. sys.path.insert(0, '')
  591. if covermode:
  592. # control_import_coverage(rootdir)
  593. from logilab.devtools.lib.coverage import Coverage
  594. cvg = Coverage([rootdir])
  595. cvg.erase()
  596. cvg.start()
  597. if DJANGO_FOUND and options.django:
  598. tester = DjangoTester(cvg, options)
  599. else:
  600. tester = testercls(cvg, options)
  601. if explicitfile:
  602. cmd, args = tester.testfile, (explicitfile,)
  603. elif options.testdir:
  604. cmd, args = tester.testonedir, (options.testdir, options.exitfirst)
  605. else:
  606. cmd, args = tester.testall, (options.exitfirst,)
  607. try:
  608. try:
  609. if options.profile:
  610. import hotshot
  611. prof = hotshot.Profile(options.profile)
  612. prof.runcall(cmd, *args)
  613. prof.close()
  614. print 'profile data saved in', options.profile
  615. else:
  616. cmd(*args)
  617. except SystemExit:
  618. raise
  619. except:
  620. import traceback
  621. traceback.print_exc()
  622. finally:
  623. if covermode:
  624. cvg.stop()
  625. cvg.save()
  626. tester.show_report()
  627. if covermode:
  628. print 'coverage information stored, use it with pycoverage -ra'
  629. sys.exit(tester.errcode)
  630. class SkipAwareTestProgram(unittest.TestProgram):
  631. # XXX: don't try to stay close to unittest.py, use optparse
  632. USAGE = """\
  633. Usage: %(progName)s [options] [test] [...]
  634. Options:
  635. -h, --help Show this message
  636. -v, --verbose Verbose output
  637. -i, --pdb Enable test failure inspection
  638. -x, --exitfirst Exit on first failure
  639. -s, --skip skip test matching this pattern (no regexp for now)
  640. -q, --quiet Minimal output
  641. --color colorize tracebacks
  642. -m, --match Run only test whose tag match this pattern
  643. -P, --profile FILE: Run the tests using cProfile and saving results
  644. in FILE
  645. Examples:
  646. %(progName)s - run default set of tests
  647. %(progName)s MyTestSuite - run suite 'MyTestSuite'
  648. %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
  649. %(progName)s MyTestCase - run all 'test*' test methods
  650. in MyTestCase
  651. """
  652. def __init__(self, module='__main__', defaultTest=None, batchmode=False,
  653. cvg=None, options=None, outstream=sys.stderr):
  654. self.batchmode = batchmode
  655. self.cvg = cvg
  656. self.options = options
  657. self.outstream = outstream
  658. super(SkipAwareTestProgram, self).__init__(
  659. module=module, defaultTest=defaultTest,
  660. testLoader=NonStrictTestLoader())
  661. def parseArgs(self, argv):
  662. self.pdbmode = False
  663. self.exitfirst = False
  664. self.skipped_patterns = []
  665. self.test_pattern = None
  666. self.tags_pattern = None
  667. self.colorize = False
  668. self.profile_name = None
  669. import getopt
  670. try:
  671. options, args = getopt.getopt(argv[1:], 'hHvixrqcp:s:m:P:',
  672. ['help', 'verbose', 'quiet', 'pdb',
  673. 'exitfirst', 'restart',
  674. 'skip=', 'color', 'match=', 'profile='])
  675. for opt, value in options:
  676. if opt in ('-h', '-H', '--help'):
  677. self.usageExit()
  678. if opt in ('-i', '--pdb'):
  679. self.pdbmode = True
  680. if opt in ('-x', '--exitfirst'):
  681. self.exitfirst = True
  682. if opt in ('-r', '--restart'):
  683. self.restart = True
  684. self.exitfirst = True
  685. if opt in ('-q', '--quiet'):
  686. self.verbosity = 0
  687. if opt in ('-v', '--verbose'):
  688. self.verbosity = 2
  689. if opt in ('-s', '--skip'):
  690. self.skipped_patterns = [pat.strip() for pat in
  691. value.split(', ')]
  692. if opt == '--color':
  693. self.colorize = True
  694. if opt in ('-m', '--match'):
  695. #self.tags_pattern = value
  696. self.options["tag_pattern"] = value
  697. if opt in ('-P', '--profile'):
  698. self.profile_name = value
  699. self.testLoader.skipped_patterns = self.skipped_patterns
  700. if len(args) == 0 and self.defaultTest is None:
  701. suitefunc = getattr(self.module, 'suite', None)
  702. if isinstance(suitefunc, (types.FunctionType,
  703. types.MethodType)):
  704. self.test = self.module.suite()
  705. else:
  706. self.test = self.testLoader.loadTestsFromModule(self.module)
  707. return
  708. if len(args) > 0:
  709. self.test_pattern = args[0]
  710. self.testNames = args
  711. else:
  712. self.testNames = (self.defaultTest, )
  713. self.createTests()
  714. except getopt.error, msg:
  715. self.usageExit(msg)
  716. def runTests(self):
  717. if self.profile_name:
  718. import cProfile
  719. cProfile.runctx('self._runTests()', globals(), locals(), self.profile_name )
  720. else:
  721. return self._runTests()
  722. def _runTests(self):
  723. self.testRunner = SkipAwareTextTestRunner(verbosity=self.verbosity,
  724. stream=self.outstream,
  725. exitfirst=self.exitfirst,
  726. pdbmode=self.pdbmode,
  727. cvg=self.cvg,
  728. test_pattern=self.test_pattern,
  729. skipped_patterns=self.skipped_patterns,
  730. colorize=self.colorize,
  731. batchmode=self.batchmode,
  732. options=self.options)
  733. def removeSucceededTests(obj, succTests):
  734. """ Recursive function that removes succTests from
  735. a TestSuite or TestCase
  736. """
  737. if isinstance(obj, unittest.TestSuite):
  738. removeSucceededTests(obj._tests, succTests)
  739. if isinstance(obj, list):
  740. for el in obj[:]:
  741. if isinstance(el, unittest.TestSuite):
  742. removeSucceededTests(el, succTests)
  743. elif isinstance(el, unittest.TestCase):
  744. descr = '.'.join((el.__class__.__module__,
  745. el.__class__.__name__,
  746. el._testMethodName))
  747. if descr in succTests:
  748. obj.remove(el)
  749. # take care, self.options may be None
  750. if getattr(self.options, 'restart', False):
  751. # retrieve succeeded tests from FILE_RESTART
  752. try:
  753. restartfile = open(FILE_RESTART, 'r')
  754. try:
  755. succeededtests = list(elem.rstrip('\n\r') for elem in
  756. restartfile.readlines())
  757. removeSucceededTests(self.test, succeededtests)
  758. finally:
  759. restartfile.close()
  760. except Exception, ex:
  761. raise Exception("Error while reading succeeded tests into %s: %s"
  762. % (osp.join(os.getcwd(), FILE_RESTART), ex))
  763. result = self.testRunner.run(self.test)
  764. # help garbage collection: we want TestSuite, which hold refs to every
  765. # executed TestCase, to be gc'ed
  766. del self.test
  767. if getattr(result, "debuggers", None) and \
  768. getattr(self, "pdbmode", None):
  769. start_interactive_mode(result)
  770. if not getattr(self, "batchmode", None):
  771. sys.exit(not result.wasSuccessful())
  772. self.result = result
  773. class SkipAwareTextTestRunner(unittest.TextTestRunner):
  774. def __init__(self, stream=sys.stderr, verbosity=1,
  775. exitfirst=False, pdbmode=False, cvg=None, test_pattern=None,
  776. skipped_patterns=(), colorize=False, batchmode=False,
  777. options=None):
  778. super(SkipAwareTextTestRunner, self).__init__(stream=stream,
  779. verbosity=verbosity)
  780. self.exitfirst = exitfirst
  781. self.pdbmode = pdbmode
  782. self.cvg = cvg
  783. self.test_pattern = test_pattern
  784. self.skipped_patterns = skipped_patterns
  785. self.colorize = colorize
  786. self.batchmode = batchmode
  787. self.options = options
  788. def _this_is_skipped(self, testedname):
  789. return any([(pat in testedname) for pat in self.skipped_patterns])
  790. def _runcondition(self, test, skipgenerator=True):
  791. if isinstance(test, testlib.InnerTest):
  792. testname = test.name
  793. else:
  794. if isinstance(test, testlib.TestCase):
  795. meth = test._get_test_method()
  796. func = meth.im_func
  797. testname = '%s.%s' % (meth.im_class.__name__, func.__name__)
  798. elif isinstance(test, types.FunctionType):
  799. func = test
  800. testname = func.__name__
  801. elif isinstance(test, types.MethodType):
  802. func = test.im_func
  803. testname = '%s.%s' % (test.im_class.__name__, func.__name__)
  804. else:
  805. return True # Not sure when this happens
  806. if testlib.is_generator(test) and skipgenerator:
  807. return self.does_match_tags(test) # Let inner tests decide at run time
  808. if self._this_is_skipped(testname):
  809. return False # this was explicitly skipped
  810. if self.test_pattern is not None:
  811. try:
  812. classpattern, testpattern = self.test_pattern.split('.')
  813. klass, name = testname.split('.')
  814. if classpattern not in klass or testpattern not in name:
  815. return False
  816. except ValueError:
  817. if self.test_pattern not in testname:
  818. return False
  819. return self.does_match_tags(test)
  820. def does_match_tags(self, test):
  821. if self.options is not None:
  822. tags_pattern = getattr(self.options, 'tags_pattern', None)
  823. if tags_pattern is not None:
  824. tags = getattr(test, 'tags', testlib.Tags())
  825. if tags.inherit and isinstance(test, types.MethodType):
  826. tags = tags | getattr(test.im_class, 'tags', testlib.Tags())
  827. return tags.match(tags_pattern)
  828. return True # no pattern
  829. def _makeResult(self):
  830. return testlib.SkipAwareTestResult(self.stream, self.descriptions,
  831. self.verbosity, self.exitfirst,
  832. self.pdbmode, self.cvg, self.colorize)
  833. def run(self, test):
  834. "Run the given test case or test suite."
  835. result = self._makeResult()
  836. startTime = time()
  837. test(result, runcondition=self._runcondition, options=self.options)
  838. stopTime = time()
  839. timeTaken = stopTime - startTime
  840. result.printErrors()
  841. if not self.batchmode:
  842. self.stream.writeln(result.separator2)
  843. run = result.testsRun
  844. self.stream.writeln("Ran %d test%s in %.3fs" %
  845. (run, run != 1 and "s" or "", timeTaken))
  846. self.stream.writeln()
  847. if not result.wasSuccessful():
  848. if self.colorize:
  849. self.stream.write(textutils.colorize_ansi("FAILED", color='red'))
  850. else:
  851. self.stream.write("FAILED")
  852. else:
  853. if self.colorize:
  854. self.stream.write(textutils.colorize_ansi("OK", color='green'))
  855. else:
  856. self.stream.write("OK")
  857. failed, errored, skipped = map(len, (result.failures,
  858. result.errors,
  859. result.skipped))
  860. det_results = []
  861. for name, value in (("failures", result.failures),
  862. ("errors",result.errors),
  863. ("skipped", result.skipped)):
  864. if value:
  865. det_results.append("%s=%i" % (name, len(value)))
  866. if det_results:
  867. self.stream.write(" (")
  868. self.stream.write(', '.join(det_results))
  869. self.stream.write(")")
  870. self.stream.writeln("")
  871. return result
  872. class NonStrictTestLoader(unittest.TestLoader):
  873. """
  874. Overrides default testloader to be able to omit classname when
  875. specifying tests to run on command line.
  876. For example, if the file test_foo.py contains ::
  877. class FooTC(TestCase):
  878. def test_foo1(self): # ...
  879. def test_foo2(self): # ...
  880. def test_bar1(self): # ...
  881. class BarTC(TestCase):
  882. def test_bar2(self): # ...
  883. 'python test_foo.py' will run the 3 tests in FooTC
  884. 'python test_foo.py FooTC' will run the 3 tests in FooTC
  885. 'python test_foo.py test_foo' will run test_foo1 and test_foo2
  886. 'python test_foo.py test_foo1' will run test_foo1
  887. 'python test_foo.py test_bar' will run FooTC.test_bar1 and BarTC.test_bar2
  888. """
  889. def __init__(self):
  890. self.skipped_patterns = ()
  891. # some magic here to accept empty list by extending
  892. # and to provide callable capability
  893. def loadTestsFromNames(self, names, module=None):
  894. suites = []
  895. for name in names:
  896. suites.extend(self.loadTestsFromName(name, module))
  897. return self.suiteClass(suites)
  898. def _collect_tests(self, module):
  899. tests = {}
  900. for obj in vars(module).values():
  901. if (issubclass(type(obj), (types.ClassType, type)) and
  902. issubclass(obj, unittest.TestCase)):
  903. classname = obj.__name__
  904. if classname[0] == '_' or self._this_is_skipped(classname):
  905. continue
  906. methodnames = []
  907. # obj is a TestCase class
  908. for attrname in dir(obj):
  909. if attrname.startswith(self.testMethodPrefix):
  910. attr = getattr(obj, attrname)
  911. if callable(attr):
  912. methodnames.append(attrname)
  913. # keep track of class (obj) for convenience
  914. tests[classname] = (obj, methodnames)
  915. return tests
  916. def loadTestsFromSuite(self, module, suitename):
  917. try:
  918. suite = getattr(module, suitename)()
  919. except AttributeError:
  920. return []
  921. assert hasattr(suite, '_tests'), \
  922. "%s.%s is not a valid TestSuite" % (module.__name__, suitename)
  923. # python2.3 does not implement __iter__ on suites, we need to return
  924. # _tests explicitly
  925. return suite._tests
  926. def loadTestsFromName(self, name, module=None):
  927. parts = name.split('.')
  928. if module is None or len(parts) > 2:
  929. # let the base class do its job here
  930. return [super(NonStrictTestLoader, self).loadTestsFromName(name)]
  931. tests = self._collect_tests(module)
  932. collected = []
  933. if len(parts) == 1:
  934. pattern = parts[0]
  935. if callable(getattr(module, pattern, None)
  936. ) and pattern not in tests:
  937. # consider it as a suite
  938. return self.loadTestsFromSuite(module, pattern)
  939. if pattern in tests:
  940. # case python unittest_foo.py MyTestTC
  941. klass, methodnames = tests[pattern]
  942. for methodname in methodnames:
  943. collected = [klass(methodname)
  944. for methodname in methodnames]
  945. else:
  946. # case python unittest_foo.py something
  947. for klass, methodnames in tests.values():
  948. # skip methodname if matched by skipped_patterns
  949. for skip_pattern in self.skipped_patterns:
  950. methodnames = [methodname
  951. for methodname in methodnames
  952. if skip_pattern not in methodname]
  953. collected += [klass(methodname)
  954. for methodname in methodnames
  955. if pattern in methodname]
  956. elif len(parts) == 2:
  957. # case "MyClass.test_1"
  958. classname, pattern = parts
  959. klass, methodnames = tests.get(classname, (None, []))
  960. for methodname in methodnames:
  961. collected = [klass(methodname) for methodname in methodnames
  962. if pattern in methodname]
  963. return collected
  964. def _this_is_skipped(self, testedname):
  965. return any([(pat in testedname) for pat in self.skipped_patterns])
  966. def getTestCaseNames(self, testCaseClass):
  967. """Return a sorted sequence of method names found within testCaseClass
  968. """
  969. is_skipped = self._this_is_skipped
  970. classname = testCaseClass.__name__
  971. if classname[0] == '_' or is_skipped(classname):
  972. return []
  973. testnames = super(NonStrictTestLoader, self).getTestCaseNames(
  974. testCaseClass)
  975. return [testname for testname in testnames if not is_skipped(testname)]
  976. def _ts_run(self, result, runcondition=None, options=None):
  977. self._wrapped_run(result,runcondition=runcondition, options=options)
  978. self._tearDownPreviousClass(None, result)
  979. self._handleModuleTearDown(result)
  980. return result
  981. def _ts_wrapped_run(self, result, debug=False, runcondition=None, options=None):
  982. for test in self:
  983. if result.shouldStop:
  984. break
  985. if unittest_suite._isnotsuite(test):
  986. self._tearDownPreviousClass(test, result)
  987. self._handleModuleFixture(test, result)
  988. self._handleClassSetUp(test, result)
  989. result._previousTestClass = test.__class__
  990. if (getattr(test.__class__, '_classSetupFailed', False) or
  991. getattr(result, '_moduleSetUpFailed', False)):
  992. continue
  993. if hasattr(test, '_wrapped_run'):
  994. try:
  995. test._wrapped_run(result, debug, runcondition=runcondition, options=options)
  996. except TypeError:
  997. test._wrapped_run(result, debug)
  998. elif not debug:
  999. try:
  1000. test(result, runcondition, options)
  1001. except TypeError:
  1002. test(result)
  1003. else:
  1004. test.debug()
  1005. def enable_dbc(*args):
  1006. """
  1007. Without arguments, return True if contracts can be enabled and should be
  1008. enabled (see option -d), return False otherwise.
  1009. With arguments, return False if contracts can't or shouldn't be enabled,
  1010. otherwise weave ContractAspect with items passed as arguments.
  1011. """
  1012. if not ENABLE_DBC:
  1013. return False
  1014. try:
  1015. from logilab.aspects.weaver import weaver
  1016. from logilab.aspects.lib.contracts import ContractAspect
  1017. except ImportError:
  1018. sys.stderr.write(
  1019. 'Warning: logilab.aspects is not available. Contracts disabled.')
  1020. return False
  1021. for arg in args:
  1022. weaver.weave_module(arg, ContractAspect)
  1023. return True
  1024. # monkeypatch unittest and doctest (ouch !)
  1025. unittest._TextTestResult = testlib.SkipAwareTestResult
  1026. unittest.TextTestRunner = SkipAwareTextTestRunner
  1027. unittest.TestLoader = NonStrictTestLoader
  1028. unittest.TestProgram = SkipAwareTestProgram
  1029. if sys.version_info >= (2, 4):
  1030. doctest.DocTestCase.__bases__ = (testlib.TestCase,)
  1031. # XXX check python2.6 compatibility
  1032. #doctest.DocTestCase._cleanups = []
  1033. #doctest.DocTestCase._out = []
  1034. else:
  1035. unittest.FunctionTestCase.__bases__ = (testlib.TestCase,)
  1036. unittest.TestSuite.run = _ts_run
  1037. unittest.TestSuite._wrapped_run = _ts_wrapped_run