unit_tests.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test/unit_tests.py: Unit tests for the MMGen suite
  20. """
  21. import sys,os,time
  22. repo_root = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),os.pardir)))
  23. os.chdir(repo_root)
  24. sys.path[0] = repo_root
  25. os.environ['MMGEN_TEST_SUITE'] = '1'
  26. # Import these _after_ prepending repo_root to sys.path
  27. from mmgen.common import *
  28. opts_data = {
  29. 'text': {
  30. 'desc': "Unit tests for the MMGen suite",
  31. 'usage':'[options] [tests]',
  32. 'options': """
  33. -h, --help Print this help message
  34. -f, --fast Speed up execution by reducing rounds on some tests
  35. -l, --list List available tests
  36. -n, --names Print command names instead of descriptions
  37. -q, --quiet Produce quieter output
  38. -v, --verbose Produce more verbose output
  39. """,
  40. 'notes': """
  41. If no test is specified, all available tests are run
  42. """
  43. }
  44. }
  45. sys.argv = [sys.argv[0]] + ['--skip-cfg-file'] + sys.argv[1:]
  46. cmd_args = opts.init(opts_data)
  47. def exit_msg():
  48. t = int(time.time()) - start_time
  49. gmsg('All requested tests finished OK, elapsed time: {:02}:{:02}'.format(t//60,t%60))
  50. all_tests = [fn[3:-3] for fn in os.listdir(os.path.join(repo_root,'test','unit_tests_d')) if fn[:3] == 'ut_']
  51. start_time = int(time.time())
  52. if opt.list:
  53. Die(0,' '.join(all_tests))
  54. try:
  55. for test in cmd_args:
  56. if test not in all_tests:
  57. die(1,"'{}': test not recognized".format(test))
  58. for test in (cmd_args or all_tests):
  59. exec('from test.unit_tests_d.ut_{m} import {m}'.format(m=test))
  60. gmsg('Running unit test {}'.format(test))
  61. t = globals()[test]()
  62. if not t.run_test(test):
  63. rdie(1,'Unit test {!r} failed'.format(test))
  64. exec('del {}'.format(test))
  65. exit_msg()
  66. except KeyboardInterrupt:
  67. die(1,green('\nExiting at user request'))