javatest.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. # Federico Pellegrin, 2017 (fedepell)
  4. """
  5. Provides Java Unit test support using :py:class:`waflib.Tools.waf_unit_test.utest`
  6. task via the **javatest** feature.
  7. This gives the possibility to run unit test and have them integrated into the
  8. standard waf unit test environment. It has been tested with TestNG and JUnit
  9. but should be easily expandable to other frameworks given the flexibility of
  10. ut_str provided by the standard waf unit test environment.
  11. Example usage:
  12. def options(opt):
  13. opt.load('java waf_unit_test javatest')
  14. def configure(conf):
  15. conf.load('java javatest')
  16. def build(bld):
  17. [ ... mainprog is built here ... ]
  18. bld(features = 'javac javatest',
  19. srcdir = 'test/',
  20. outdir = 'test',
  21. sourcepath = ['test'],
  22. classpath = [ 'src' ],
  23. basedir = 'test',
  24. use = ['JAVATEST', 'mainprog'], # mainprog is the program being tested in src/
  25. ut_str = 'java -cp ${CLASSPATH} ${JTRUNNER} ${SRC}',
  26. jtest_source = bld.path.ant_glob('test/*.xml'),
  27. )
  28. At command line the CLASSPATH where to find the testing environment and the
  29. test runner (default TestNG) that will then be seen in the environment as
  30. CLASSPATH_JAVATEST (then used for use) and JTRUNNER and can be used for
  31. dependencies and ut_str generation.
  32. Example configure for TestNG:
  33. waf configure --jtpath=/tmp/testng-6.12.jar:/tmp/jcommander-1.71.jar --jtrunner=org.testng.TestNG
  34. or as default runner is TestNG:
  35. waf configure --jtpath=/tmp/testng-6.12.jar:/tmp/jcommander-1.71.jar
  36. Example configure for JUnit:
  37. waf configure --jtpath=/tmp/junit.jar --jtrunner=org.junit.runner.JUnitCore
  38. The runner class presence on the system is checked for at configuration stage.
  39. """
  40. import os
  41. from waflib import Task, TaskGen, Options
  42. @TaskGen.feature('javatest')
  43. @TaskGen.after_method('apply_java', 'use_javac_files', 'set_classpath')
  44. def make_javatest(self):
  45. """
  46. Creates a ``utest`` task with a populated environment for Java Unit test execution
  47. """
  48. tsk = self.create_task('utest')
  49. tsk.set_run_after(self.javac_task)
  50. # Put test input files as waf_unit_test relies on that for some prints and log generation
  51. # If jtest_source is there, this is specially useful for passing XML for TestNG
  52. # that contain test specification, use that as inputs, otherwise test sources
  53. if getattr(self, 'jtest_source', None):
  54. tsk.inputs = self.to_nodes(self.jtest_source)
  55. else:
  56. if self.javac_task.srcdir[0].exists():
  57. tsk.inputs = self.javac_task.srcdir[0].ant_glob('**/*.java', remove=False)
  58. if getattr(self, 'ut_str', None):
  59. self.ut_run, lst = Task.compile_fun(self.ut_str, shell=getattr(self, 'ut_shell', False))
  60. tsk.vars = lst + tsk.vars
  61. if getattr(self, 'ut_cwd', None):
  62. if isinstance(self.ut_cwd, str):
  63. # we want a Node instance
  64. if os.path.isabs(self.ut_cwd):
  65. self.ut_cwd = self.bld.root.make_node(self.ut_cwd)
  66. else:
  67. self.ut_cwd = self.path.make_node(self.ut_cwd)
  68. else:
  69. self.ut_cwd = self.bld.bldnode
  70. # Get parent CLASSPATH and add output dir of test, we run from wscript dir
  71. # We have to change it from list to the standard java -cp format (: separated)
  72. tsk.env.CLASSPATH = ':'.join(self.env.CLASSPATH) + ':' + self.outdir.abspath()
  73. if not self.ut_cwd.exists():
  74. self.ut_cwd.mkdir()
  75. if not hasattr(self, 'ut_env'):
  76. self.ut_env = dict(os.environ)
  77. def configure(ctx):
  78. cp = ctx.env.CLASSPATH or '.'
  79. if getattr(Options.options, 'jtpath', None):
  80. ctx.env.CLASSPATH_JAVATEST = getattr(Options.options, 'jtpath').split(':')
  81. cp += ':' + getattr(Options.options, 'jtpath')
  82. if getattr(Options.options, 'jtrunner', None):
  83. ctx.env.JTRUNNER = getattr(Options.options, 'jtrunner')
  84. if ctx.check_java_class(ctx.env.JTRUNNER, with_classpath=cp):
  85. ctx.fatal('Could not run test class %r' % ctx.env.JTRUNNER)
  86. def options(opt):
  87. opt.add_option('--jtpath', action='store', default='', dest='jtpath',
  88. help='Path to jar(s) needed for javatest execution, colon separated, if not in the system CLASSPATH')
  89. opt.add_option('--jtrunner', action='store', default='org.testng.TestNG', dest='jtrunner',
  90. help='Class to run javatest test [default: org.testng.TestNG]')