nitunit: allow testing from external files called test-suites.
[nit.git] / src / nitunit.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Program to extract and execute unit tests from nit source files
16 module nitunit
17
18 import testing
19
20 var toolcontext = new ToolContext
21
22 toolcontext.option_context.add_option(toolcontext.opt_full, toolcontext.opt_output, toolcontext.opt_dir, toolcontext.opt_noact, toolcontext.opt_pattern, toolcontext.opt_file)
23 toolcontext.tooldescription = "Usage: nitunit [OPTION]... <file.nit>...\nExecutes the unit tests from Nit source files."
24
25 toolcontext.process_options(args)
26 var args = toolcontext.option_context.rest
27
28 var model = new Model
29 var modelbuilder = new ModelBuilder(model, toolcontext)
30
31 var mmodules = modelbuilder.parse(args)
32 modelbuilder.run_phases
33
34 var page = new HTMLTag("testsuites")
35
36 if toolcontext.opt_full.value then mmodules = model.mmodules
37
38 for m in mmodules do
39 page.add modelbuilder.test_markdown(m)
40 page.add modelbuilder.test_unit(m)
41 end
42
43 var file = toolcontext.opt_output.value
44 if file == null then file = "nitunit.xml"
45 page.write_to_file(file)
46 print "Results saved in {file}"
47 # print docunits results
48 print "\nDocUnits:"
49 if modelbuilder.unit_entities == 0 then
50 print "No doc units found"
51 else if modelbuilder.failed_entities == 0 and not toolcontext.opt_noact.value then
52 print "DocUnits Success"
53 end
54 print "Entities: {modelbuilder.total_entities}; Documented ones: {modelbuilder.doc_entities}; With nitunits: {modelbuilder.unit_entities}; Failures: {modelbuilder.failed_entities}"
55 # print testsuites results
56 print "\nTestSuites:"
57 if modelbuilder.total_tests == 0 then
58 print "No test cases found"
59 else if modelbuilder.failed_tests == 0 and not toolcontext.opt_noact.value then
60 print "TestSuites Success"
61 end
62 print "Class suites: {modelbuilder.total_classes}; Test Cases: {modelbuilder.total_tests}; Failures: {modelbuilder.failed_tests}"