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