runtests.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python
  2. """
  3. python runtests.py -py
  4. Use py.test to run tests (more useful for debugging)
  5. python runtests.py -coverage
  6. Generate test coverage report. Statistics are written to /tmp
  7. python runtests.py -profile
  8. Generate profile stats (this is much slower)
  9. python runtests.py -nogmpy
  10. Run tests without using GMPY even if it exists
  11. python runtests.py -strict
  12. Enforce extra tests in normalize()
  13. python runtests.py -local
  14. Insert '../..' at the beginning of sys.path to use local mpmath
  15. python runtests.py -skip ...
  16. Skip tests from the listed modules
  17. Additional arguments are used to filter the tests to run. Only files that have
  18. one of the arguments in their name are executed.
  19. """
  20. import sys, os, traceback
  21. profile = False
  22. if "-profile" in sys.argv:
  23. sys.argv.remove('-profile')
  24. profile = True
  25. coverage = False
  26. if "-coverage" in sys.argv:
  27. sys.argv.remove('-coverage')
  28. coverage = True
  29. if "-nogmpy" in sys.argv:
  30. sys.argv.remove('-nogmpy')
  31. os.environ['MPMATH_NOGMPY'] = 'Y'
  32. if "-strict" in sys.argv:
  33. sys.argv.remove('-strict')
  34. os.environ['MPMATH_STRICT'] = 'Y'
  35. if "-local" in sys.argv:
  36. sys.argv.remove('-local')
  37. importdir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),
  38. '../..'))
  39. else:
  40. importdir = ''
  41. # TODO: add a flag for this
  42. testdir = ''
  43. def testit(importdir='', testdir='', exit_on_fail=False):
  44. """Run all tests in testdir while importing from importdir."""
  45. if importdir:
  46. sys.path.insert(1, importdir)
  47. if testdir:
  48. sys.path.insert(1, testdir)
  49. import os.path
  50. import mpmath
  51. print("mpmath imported from %s" % os.path.dirname(mpmath.__file__))
  52. print("mpmath backend: %s" % mpmath.libmp.backend.BACKEND)
  53. print("mpmath mp class: %s" % repr(mpmath.mp))
  54. print("mpmath version: %s" % mpmath.__version__)
  55. print("Python version: %s" % sys.version)
  56. print("")
  57. if "-py" in sys.argv:
  58. sys.argv.remove('-py')
  59. import py
  60. py.test.cmdline.main()
  61. else:
  62. import glob
  63. from timeit import default_timer as clock
  64. modules = []
  65. args = sys.argv[1:]
  66. excluded = []
  67. if '-skip' in args:
  68. excluded = args[args.index('-skip')+1:]
  69. args = args[:args.index('-skip')]
  70. # search for tests in directory of this file if not otherwise specified
  71. if not testdir:
  72. pattern = os.path.dirname(sys.argv[0])
  73. else:
  74. pattern = testdir
  75. if pattern:
  76. pattern += '/'
  77. pattern += 'test*.py'
  78. # look for tests (respecting specified filter)
  79. for f in glob.glob(pattern):
  80. name = os.path.splitext(os.path.basename(f))[0]
  81. # If run as a script, only run tests given as args, if any are given
  82. if args and __name__ == "__main__":
  83. ok = False
  84. for arg in args:
  85. if arg in name:
  86. ok = True
  87. break
  88. if not ok:
  89. continue
  90. elif name in excluded:
  91. continue
  92. module = __import__(name)
  93. priority = module.__dict__.get('priority', 100)
  94. if priority == 666:
  95. modules = [[priority, name, module]]
  96. break
  97. modules.append([priority, name, module])
  98. # execute tests
  99. modules.sort()
  100. tstart = clock()
  101. for priority, name, module in modules:
  102. print(name)
  103. for f in sorted(module.__dict__.keys()):
  104. if f.startswith('test_'):
  105. if coverage and ('numpy' in f):
  106. continue
  107. sys.stdout.write(" " + f[5:].ljust(25) + " ")
  108. t1 = clock()
  109. try:
  110. module.__dict__[f]()
  111. except:
  112. etype, evalue, trb = sys.exc_info()
  113. if etype in (KeyboardInterrupt, SystemExit):
  114. raise
  115. print("")
  116. print("TEST FAILED!")
  117. print("")
  118. traceback.print_exc()
  119. if exit_on_fail:
  120. return
  121. t2 = clock()
  122. print("ok " + " " + ("%.7f" % (t2-t1)) + " s")
  123. tend = clock()
  124. print("")
  125. print("finished tests in " + ("%.2f" % (tend-tstart)) + " seconds")
  126. # clean sys.path
  127. if importdir:
  128. sys.path.remove(importdir)
  129. if testdir:
  130. sys.path.remove(testdir)
  131. if __name__ == '__main__':
  132. if profile:
  133. import cProfile
  134. cProfile.run("testit('%s', '%s')" % (importdir, testdir), sort=1)
  135. elif coverage:
  136. import trace
  137. tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
  138. trace=0, count=1)
  139. tracer.run('testit(importdir, testdir)')
  140. r = tracer.results()
  141. r.write_results(show_missing=True, summary=True, coverdir="/tmp")
  142. else:
  143. testit(importdir, testdir)