unit_tests.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. from tests_header import repo_root
  23. from mmgen.common import *
  24. opts_data = {
  25. 'text': {
  26. 'desc': "Unit tests for the MMGen suite",
  27. 'usage':'[options] [tests]',
  28. 'options': """
  29. -h, --help Print this help message
  30. -f, --fast Speed up execution by reducing rounds on some tests
  31. -l, --list List available tests
  32. -n, --names Print command names instead of descriptions
  33. -q, --quiet Produce quieter output
  34. -v, --verbose Produce more verbose output
  35. """,
  36. 'notes': """
  37. If no test is specified, all available tests are run
  38. """
  39. }
  40. }
  41. sys.argv = [sys.argv[0]] + ['--skip-cfg-file'] + sys.argv[1:]
  42. cmd_args = opts.init(opts_data)
  43. def exit_msg():
  44. t = int(time.time()) - start_time
  45. gmsg('All requested tests finished OK, elapsed time: {:02}:{:02}'.format(t//60,t%60))
  46. all_tests = [fn[3:-3] for fn in os.listdir(os.path.join(repo_root,'test','unit_tests_d')) if fn[:3] == 'ut_']
  47. start_time = int(time.time())
  48. if opt.list:
  49. Die(0,' '.join(all_tests))
  50. try:
  51. for test in cmd_args:
  52. if test not in all_tests:
  53. die(1,"'{}': test not recognized".format(test))
  54. import importlib
  55. for test in (cmd_args or all_tests):
  56. modname = 'test.unit_tests_d.ut_{}'.format(test)
  57. mod = importlib.import_module(modname)
  58. gmsg('Running unit test {}'.format(test))
  59. if not mod.unit_test().run_test(test):
  60. rdie(1,'Unit test {!r} failed'.format(test))
  61. del mod
  62. exit_msg()
  63. except KeyboardInterrupt:
  64. die(1,green('\nExiting at user request'))