runtests.py 4.7 KB

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