main.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # -*- coding: utf-8 -*-
  2. """!@package grass.gunittest.main
  3. @brief GRASS Python testing framework module for running from command line
  4. Copyright (C) 2014 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS GIS
  7. for details.
  8. @author Vaclav Petras
  9. """
  10. import os
  11. import sys
  12. from unittest.main import TestProgram, USAGE_AS_MAIN
  13. TestProgram.USAGE = USAGE_AS_MAIN
  14. from .loader import GrassTestLoader
  15. from .runner import GrassTestRunner
  16. from .invoker import GrassTestFilesInvoker
  17. from .utils import silent_rmtree
  18. import grass.script.core as gcore
  19. class GrassTestProgram(TestProgram):
  20. """A class to be used by individual test files (wrapped in the function)"""
  21. def __init__(self, exit_at_end, grass_location, clean_outputs=True,
  22. unittest_argv=None, module=None,
  23. verbosity=1,
  24. failfast=None, catchbreak=None):
  25. """Prepares the tests in GRASS way and then runs the tests.
  26. :param bool clean_outputs: if outputs in mapset and in ?
  27. """
  28. self.test = None
  29. self.grass_location = grass_location
  30. # it is unclear what the exact behavior is in unittest
  31. # buffer stdout and stderr during tests
  32. buffer_stdout_stderr = False
  33. grass_loader = GrassTestLoader(grass_location=self.grass_location)
  34. grass_runner = GrassTestRunner(verbosity=verbosity,
  35. failfast=failfast,
  36. buffer=buffer_stdout_stderr)
  37. super(GrassTestProgram, self).__init__(module=module,
  38. argv=unittest_argv,
  39. testLoader=grass_loader,
  40. testRunner=grass_runner,
  41. exit=exit_at_end,
  42. verbosity=verbosity,
  43. failfast=failfast,
  44. catchbreak=catchbreak,
  45. buffer=buffer_stdout_stderr)
  46. def test():
  47. """Run a test of a module.
  48. """
  49. # TODO: put the link to to the report only if available
  50. # TODO: how to disable Python code coverage for module and C tests?
  51. # TODO: we probably need to have different test functions for C, Python modules, and Python code
  52. # TODO: combine the results using python -m coverage --help | grep combine
  53. # TODO: function to anonymize/beautify file names (in content and actual filenames)
  54. doing_coverage = False
  55. try:
  56. import coverage
  57. doing_coverage = True
  58. cov = coverage.coverage(omit="*testsuite*")
  59. cov.start()
  60. except ImportError:
  61. pass
  62. # TODO: add some message somewhere
  63. # TODO: enable passing omit to exclude also gunittest or nothing
  64. program = GrassTestProgram(module='__main__', exit_at_end=False, grass_location='all')
  65. # TODO: check if we are in the directory where the test file is
  66. # this will ensure that data directory is available when it is requested
  67. if doing_coverage:
  68. cov.stop()
  69. cov.html_report(directory='testcodecoverage')
  70. # TODO: is sys.exit the right thing here
  71. sys.exit(not program.result.wasSuccessful())
  72. # TODO: test or main? test looks more general
  73. # unittest has main() but doctest has testmod()
  74. main = test
  75. def discovery():
  76. """Recursively find all tests in testsuite directories and run them
  77. Everything is imported and runs in this process.
  78. Runs using::
  79. python main.py discovery [start_directory]
  80. """
  81. doing_coverage = False
  82. try:
  83. import coverage
  84. doing_coverage = True
  85. cov = coverage.coverage(omit="*testsuite*")
  86. cov.start()
  87. except ImportError:
  88. pass
  89. # TODO: add some message somewhere
  90. program = GrassTestProgram(grass_location='nc', exit_at_end=False)
  91. if doing_coverage:
  92. cov.stop()
  93. cov.html_report(directory='testcodecoverage')
  94. sys.exit(not program.result.wasSuccessful())
  95. # TODO: makefile rule should depend on the whole build
  96. # TODO: create a full interface (using grass parser or argparse)
  97. if __name__ == '__main__':
  98. if len(sys.argv) == 4:
  99. gisdbase = sys.argv[1]
  100. location = sys.argv[2]
  101. location_shortcut = sys.argv[3]
  102. elif len(sys.argv) == 3:
  103. location = sys.argv[1]
  104. location_shortcut = sys.argv[2]
  105. gisdbase = gcore.gisenv()['GISDBASE']
  106. else:
  107. sys.stderr.write("Usage: %s [gisdbase] location location_shortcut\n" % sys.argv[0])
  108. sys.exit(1)
  109. assert gisdbase
  110. if not os.path.exists(gisdbase):
  111. sys.stderr.write("GISDBASE <%s> does not exist\n" % gisdbase)
  112. sys.exit(1)
  113. results_dir = 'testreport'
  114. silent_rmtree(results_dir) # TODO: too brute force?
  115. invoker = GrassTestFilesInvoker(start_dir='.')
  116. # we can just iterate over all locations available in database
  117. # but the we don't know the right location label/shortcut
  118. invoker.run_in_location(gisdbase=gisdbase,
  119. location=location,
  120. location_shortcut=location_shortcut,
  121. results_dir=results_dir)