nitunit: set NIT_TESTING_PATH before running a test suite
[nit.git] / src / testing / testing_suite.nit
index b66c9ab..e788067 100644 (file)
@@ -18,10 +18,9 @@ module testing_suite
 import testing_base
 import html
 private import annotation
+private import realtime
 
 redef class ToolContext
-       # -- target-file
-       var opt_file = new OptionString("Specify test suite location", "-t", "--target-file")
        # --pattern
        var opt_pattern = new OptionString("Only run test case with name that match pattern", "-p", "--pattern")
        # --autosav
@@ -123,19 +122,33 @@ class TestSuite
        # Test to be executed after the whole test suite.
        var after_module: nullable TestCase = null
 
-       fun show_status
-       do
+       # Display test suite status in std-out.
+       fun show_status do
                toolcontext.show_unit_status("Test-suite of module " + mmodule.full_name, test_cases)
        end
 
        # Execute the test suite
        fun run do
+               set_env
                show_status
                if not toolcontext.test_dir.file_exists then
                        toolcontext.test_dir.mkdir
                end
                write_to_nit
                compile
+               if failure != null then
+                       for case in test_cases do
+                               case.is_done = true
+                               case.error = "Compilation Error"
+                               case.raw_output = failure
+                               toolcontext.modelbuilder.failed_tests += 1
+                               toolcontext.clear_progress_bar
+                               toolcontext.show_unit(case)
+                       end
+                       show_status
+                       print ""
+                       return
+               end
                toolcontext.info("Execute test-suite {mmodule.name}", 1)
                var before_module = self.before_module
                if not before_module == null then before_module.run
@@ -170,14 +183,7 @@ class TestSuite
        fun to_xml: HTMLTag do
                var n = new HTMLTag("testsuite")
                n.attr("package", mmodule.name)
-               var failure = self.failure
-               if failure != null then
-                       var f = new HTMLTag("failure")
-                       f.attr("message", failure.to_s)
-                       n.add f
-               else
-                       for test in test_cases do n.add test.to_xml
-               end
+               for test in test_cases do n.add test.to_xml
                return n
        end
 
@@ -204,14 +210,16 @@ class TestSuite
                var f = new FileReader.open("{file}.out")
                var msg = f.read_all
                f.close
-               # set test case result
-               var loc = mmodule.location
                if res != 0 then
                        failure = msg
-                       toolcontext.warning(loc, "failure", "FAILURE: {mmodule.name} (in file {file}.nit): {msg}")
-                       toolcontext.modelbuilder.failed_tests += 1
                end
-               toolcontext.check_errors
+       end
+
+       # Set environment variables for test suite execution
+       fun set_env do
+               var loc = mmodule.location.file
+               if loc == null then return
+               toolcontext.set_testing_path(loc.filename)
        end
 
        # Error occured during test-suite compilation.
@@ -259,7 +267,10 @@ class TestCase
                var method_name = test_method.name
                var test_file = test_suite.test_file
                var res_name = "{test_file}_{method_name.escape_to_c}"
+               var clock = new Clock
                var res = toolcontext.safe_exec("{test_file}.bin {method_name} > '{res_name}.out1' 2>&1 </dev/null")
+               if not toolcontext.opt_no_time.value then real_time = clock.total
+
                var raw_output = "{res_name}.out1".to_path.read_all
                self.raw_output = raw_output
                # set test case result
@@ -308,12 +319,13 @@ class TestCase
        end
 
        redef fun xml_classname do
-               var mclassdef = test_method.mclassdef
-               return "nitunit." + mclassdef.mmodule.full_name + "." + mclassdef.mclass.full_name
+               var a = test_method.full_name.split("$")
+               return "nitunit.{a[0]}.{a[1]}"
        end
 
        redef fun xml_name do
-               return test_method.mproperty.full_name
+               var a = test_method.full_name.split("$")
+               return a[2]
        end
 end