gunittest_running_tests.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Running the test framework of GRASS GIS
  2. =======================================
  3. This is an advanced guide to running tests of GRASS GIS using GRASS
  4. testing framework (`gunittest`). For introduction to this topic,
  5. go to :ref:`test-general`.
  6. .. _running-tests-report:
  7. Running tests and creating report
  8. ---------------------------------
  9. To test before commit, run all tests using testing framework.
  10. First start GRASS GIS session and go to the root directory of your
  11. GRASS GIS source code copy::
  12. cd my/grass/source/code/root
  13. Then execute::
  14. python -m grass.gunittest.main --location locname --location-type nc
  15. where ``locname`` is a name of location in current GRASS GIS data(base) directory
  16. (GISDBASE) and ``nc`` is a location specified by individual test files
  17. (the later is not yet fully implemented, so just put there ``nc`` every time).
  18. ``grass.gunittest.main`` writes a text summary to standard output and
  19. it creates an HTML report from all tests in all ``testsuite`` directories inside
  20. the directory tree. The report is placed in ``testreport`` by default.
  21. Open file ``testreport/index.html`` in you web browser to inspect it.
  22. To execute just part of the tests when fixing something, ``cd`` into some
  23. subdirectory, e.g. ``lib`` and execute the same command as above.
  24. gain, it will execute all tests in all ``testsuite`` subdirectories and
  25. create a report.
  26. For changing GRASS GIS data(base) directory and for other parameters, see
  27. help for ``grass.gunittest.main`` module::
  28. python -m grass.gunittest.main --help
  29. Running individual test files
  30. -----------------------------
  31. To run a single test file, start GRASS session in the Location and Mapset
  32. suitable for testing and go to the directory where the test file is.
  33. Then run the file as a Python script::
  34. python test_something.py
  35. If the file is a ``gunittest``-based or ``unittest``-based test,
  36. you will receive a textual output with failed individual tests (test methods).
  37. If the file is a general Python scriptyou need to examine the output carefully
  38. as well as source code itself to see what is expected behavior.
  39. The same as for general Python scripts, applies also to Shell scripts,
  40. so you should examine the output carefully.
  41. You should execute scripts using::
  42. sh -e -x test_topology_vgeneralize.sh
  43. The ``-x`` is just to see which commands are executed but the ``-e`` flag
  44. is crucial because this is how the GRASS testing framework runs the Shell
  45. scripts. The flag causes execution to stop once some command gives a non-zero
  46. return code.
  47. Example Bash script to run be used as a cron job
  48. ------------------------------------------------
  49. .. code-block:: bash
  50. #!/bin/bash
  51. set -e # fail fast
  52. REPORTS=".../testreports"
  53. GRASSSRC=".../grass-src"
  54. # here we suppose default compilation settings of GRASS and no make install
  55. GRASSBIN="$GRASSSRC/bin.../grass71"
  56. GRASSDIST="$GRASSSRC/dist..."
  57. # necessary hardcoded GRASS paths
  58. GRASSDIST_PYTHON="$GRASSDIST/etc/python"
  59. GRASS_MULTI_RUNNER="$GRASSSRC/lib/python/gunittest/multirunner.py"
  60. GRASS_MULTI_REPORTER="$GRASSSRC/lib/python/gunittest/multireport.py"
  61. DATE_FLAGS="--utc +%Y-%m-%d-%H-%M"
  62. NOW=$(date $DATE_FLAGS)
  63. # contains last executed command stdout and stderr
  64. # here were rely on reports being absolute
  65. OUTPUT_LOGFILE="$REPORTS/output-$NOW.txt"
  66. # these are relative to REPORTS
  67. CURRENT_REPORT_BASENAME="reports_for_date-"
  68. FINAL_REPORT_DIR="summary_report"
  69. CURRENT_REPORTS_DIR="$CURRENT_REPORT_BASENAME$NOW"
  70. LOGFILE="$REPORTS/runs.log"
  71. GRASSDATA="/grassdata/tests-grassdata"
  72. echo "Nightly GRASS GIS test started: $NOW" >> $LOGFILE
  73. # compile current source code from scratch
  74. cd $GRASSSRC
  75. make distclean -j4
  76. svn up
  77. ./configure ... # or a script containing all the flags
  78. make -j4
  79. # run tests for the current source code
  80. cd $REPORTS
  81. mkdir $CURRENT_REPORTS_DIR
  82. cd $CURRENT_REPORTS_DIR
  83. python $GRASS_MULTI_RUNNER \
  84. --grassbin $GRASSBIN \
  85. --grasssrc $GRASSSRC \
  86. --grassdata $GRASSDATA \
  87. --location nc_spm_08_grass7 --location-type nc \
  88. --location other_location --location-type other_type
  89. # create overall report of all so far executed tests
  90. # the script depends on GRASS but just Python part is enough
  91. export PYTHONPATH="$GRASSDIST_PYTHON:$PYTHONPATH"
  92. python $GRASS_MULTI_REPORTER --output $FINAL_REPORT_DIR \
  93. $CURRENT_REPORT_BASENAME*/*
  94. # although we cannot be sure the tests were executed was successfully
  95. # so publish or archive results
  96. rsync -rtvu --delete $REPORTS/ "/var/www/html/grassgistestreports"
  97. echo "Nightly ($NOW) GRASS GIS test finished: $(date $DATE_FLAGS)" >> $LOGFILE
  98. A script similar to this one can be used as a cron job, on most Linux systems
  99. using ``crontab -e`` and adding a line similar to the following one::
  100. 0 4 * * 1 .../grasstests/test_grass_gis.sh
  101. Which will perform the tests every Monday at 4:00 in the morning (local time).
  102. Particular script and frequency depends on what you want to test and
  103. how many resources you want to use.