A test case is a unit test considering only a MMethodDef.

Introduced properties

private var _after: Array[MMethodDef]

nitc :: TestCase :: _after

Cases to execute after this one
private var _before: Array[MMethodDef]

nitc :: TestCase :: _before

Cases to execute before this one
private var _test_method: MMethodDef

nitc :: TestCase :: _test_method

Test method to be compiled and tested.
private var _test_suite: TestSuite

nitc :: TestCase :: _test_suite

Test suite wich self belongs to.
private var _toolcontext: ToolContext

nitc :: TestCase :: _toolcontext

ToolContext to use to display messages and find nitc bin.
fun after: Array[MMethodDef]

nitc :: TestCase :: after

Cases to execute after this one
protected fun after=(after: Array[MMethodDef])

nitc :: TestCase :: after=

Cases to execute after this one
fun before: Array[MMethodDef]

nitc :: TestCase :: before

Cases to execute before this one
protected fun before=(before: Array[MMethodDef])

nitc :: TestCase :: before=

Cases to execute before this one
init defaultinit(test_suite: TestSuite, test_method: MMethodDef, toolcontext: ToolContext)

nitc :: TestCase :: defaultinit

fun fail(message: String)

nitc :: TestCase :: fail

Make the test case fail without testing it
fun run

nitc :: TestCase :: run

Execute the test case.
fun test_method: MMethodDef

nitc :: TestCase :: test_method

Test method to be compiled and tested.
protected fun test_method=(test_method: MMethodDef)

nitc :: TestCase :: test_method=

Test method to be compiled and tested.
fun test_suite: TestSuite

nitc :: TestCase :: test_suite

Test suite wich self belongs to.
protected fun test_suite=(test_suite: TestSuite)

nitc :: TestCase :: test_suite=

Test suite wich self belongs to.
fun toolcontext: ToolContext

nitc :: TestCase :: toolcontext

ToolContext to use to display messages and find nitc bin.
protected fun toolcontext=(toolcontext: ToolContext)

nitc :: TestCase :: toolcontext=

ToolContext to use to display messages and find nitc bin.
fun write_to_nit(file: Template)

nitc :: TestCase :: write_to_nit

Generate the test unit for self in file.

Redefined properties

redef type SELF: TestCase

nitc $ TestCase :: SELF

Type of this instance, automatically specialized in every class
redef fun full_name: String

nitc $ TestCase :: full_name

The name of the unit to show in messages
redef fun location: Location

nitc $ TestCase :: location

The location of the unit test to show in messages.
redef fun xml_classname: String

nitc $ TestCase :: xml_classname

The classname attribute of the XML format.
redef fun xml_name: String

nitc $ TestCase :: xml_name

The name attribute of the XML format.

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _after: Array[MMethodDef]

nitc :: TestCase :: _after

Cases to execute after this one
private var _before: Array[MMethodDef]

nitc :: TestCase :: _before

Cases to execute before this one
private var _error: nullable String

nitc :: UnitTest :: _error

Error message occurred during test-case execution (or compilation).
private var _error_location: nullable Location

nitc :: UnitTest :: _error_location

The location where the error occurred, if it makes sense.
private var _info: nullable String

nitc :: UnitTest :: _info

Additional noteworthy information when a test success.
private var _is_done: Bool

nitc :: UnitTest :: _is_done

Flag that indicates if the unit test was compiled/run.
private var _raw_output: nullable String

nitc :: UnitTest :: _raw_output

The raw output of the execution (or compilation)
private var _real_time: Float

nitc :: UnitTest :: _real_time

Time for the execution, in seconds
private var _test_method: MMethodDef

nitc :: TestCase :: _test_method

Test method to be compiled and tested.
private var _test_suite: TestSuite

nitc :: TestCase :: _test_suite

Test suite wich self belongs to.
private var _toolcontext: ToolContext

nitc :: TestCase :: _toolcontext

ToolContext to use to display messages and find nitc bin.
private var _was_exec: Bool

nitc :: UnitTest :: _was_exec

Was the test case executed at least once?
fun after: Array[MMethodDef]

nitc :: TestCase :: after

Cases to execute after this one
protected fun after=(after: Array[MMethodDef])

nitc :: TestCase :: after=

Cases to execute after this one
fun before: Array[MMethodDef]

nitc :: TestCase :: before

Cases to execute before this one
protected fun before=(before: Array[MMethodDef])

nitc :: TestCase :: before=

Cases to execute before this one
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init defaultinit(test_suite: TestSuite, test_method: MMethodDef, toolcontext: ToolContext)

nitc :: TestCase :: defaultinit

fun error: nullable String

nitc :: UnitTest :: error

Error message occurred during test-case execution (or compilation).
fun error=(error: nullable String)

nitc :: UnitTest :: error=

Error message occurred during test-case execution (or compilation).
fun error_location: nullable Location

nitc :: UnitTest :: error_location

The location where the error occurred, if it makes sense.
fun error_location=(error_location: nullable Location)

nitc :: UnitTest :: error_location=

The location where the error occurred, if it makes sense.
fun fail(message: String)

nitc :: TestCase :: fail

Make the test case fail without testing it
abstract fun full_name: String

nitc :: UnitTest :: full_name

The name of the unit to show in messages
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
fun info: nullable String

nitc :: UnitTest :: info

Additional noteworthy information when a test success.
protected fun info=(info: nullable String)

nitc :: UnitTest :: info=

Additional noteworthy information when a test success.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
fun is_done: Bool

nitc :: UnitTest :: is_done

Flag that indicates if the unit test was compiled/run.
fun is_done=(is_done: Bool)

nitc :: UnitTest :: is_done=

Flag that indicates if the unit test was compiled/run.
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
abstract fun location: Location

nitc :: UnitTest :: location

The location of the unit test to show in messages.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun raw_output: nullable String

nitc :: UnitTest :: raw_output

The raw output of the execution (or compilation)
fun raw_output=(raw_output: nullable String)

nitc :: UnitTest :: raw_output=

The raw output of the execution (or compilation)
fun real_time: Float

nitc :: UnitTest :: real_time

Time for the execution, in seconds
fun real_time=(real_time: Float)

nitc :: UnitTest :: real_time=

Time for the execution, in seconds
fun run

nitc :: TestCase :: run

Execute the test case.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun status_tag(color: nullable Bool): String

nitc :: UnitTest :: status_tag

A colorful [OK] or [KO].
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun test_method: MMethodDef

nitc :: TestCase :: test_method

Test method to be compiled and tested.
protected fun test_method=(test_method: MMethodDef)

nitc :: TestCase :: test_method=

Test method to be compiled and tested.
fun test_suite: TestSuite

nitc :: TestCase :: test_suite

Test suite wich self belongs to.
protected fun test_suite=(test_suite: TestSuite)

nitc :: TestCase :: test_suite=

Test suite wich self belongs to.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun to_screen(more_message: nullable String, color: nullable Bool): String

nitc :: UnitTest :: to_screen

The full (color) description of the test-case.
fun to_xml: HTMLTag

nitc :: UnitTest :: to_xml

Return a <testcase> XML node in format compatible with Jenkins unit tests.
fun toolcontext: ToolContext

nitc :: TestCase :: toolcontext

ToolContext to use to display messages and find nitc bin.
protected fun toolcontext=(toolcontext: ToolContext)

nitc :: TestCase :: toolcontext=

ToolContext to use to display messages and find nitc bin.
fun was_exec: Bool

nitc :: UnitTest :: was_exec

Was the test case executed at least once?
fun was_exec=(was_exec: Bool)

nitc :: UnitTest :: was_exec=

Was the test case executed at least once?
fun write_to_nit(file: Template)

nitc :: TestCase :: write_to_nit

Generate the test unit for self in file.
abstract fun xml_classname: String

nitc :: UnitTest :: xml_classname

The classname attribute of the XML format.
abstract fun xml_name: String

nitc :: UnitTest :: xml_name

The name attribute of the XML format.
package_diagram nitc::TestCase TestCase nitc::UnitTest UnitTest nitc::TestCase->nitc::UnitTest core::Object Object nitc::UnitTest->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class UnitTest

nitc :: UnitTest

A unit test is an elementary test discovered, run and reported by nitunit.

Class definitions

nitc $ TestCase
# A test case is a unit test considering only a `MMethodDef`.
class TestCase
	super UnitTest

	# Test suite wich `self` belongs to.
	var test_suite: TestSuite

	# Test method to be compiled and tested.
	var test_method: MMethodDef

	# Cases to execute before this one
	var before = new Array[MMethodDef]

	# Cases to execute after this one
	var after = new Array[MMethodDef]

	redef fun full_name do return test_method.full_name

	redef fun location do return test_method.location

	# `ToolContext` to use to display messages and find `nitc` bin.
	var toolcontext: ToolContext

	# Generate the test unit for `self` in `file`.
	fun write_to_nit(file: Template) do
		var name = test_method.name
		file.addn "if name == \"{name}\" then"
		if test_method.mproperty.is_toplevel then
			file.addn "\t{name}"
		else
			file.addn "\tvar subject = new {test_method.mclassdef.name}.intern"
			for mmethod in before do
				file.addn "\tsubject.{mmethod.name}"
			end
			file.addn "\tsubject.{name}"
			for mmethod in after do
				file.addn "\tsubject.{mmethod.name}"
			end
		end
		file.addn "end"
	end

	# Execute the test case.
	fun run do
		toolcontext.info("Execute test-case {test_method.name}", 1)
		was_exec = true
		if toolcontext.opt_noact.value then return
		# execute
		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
		if res != 0 then
			error = "Runtime Error in file {test_file}.nit"
			toolcontext.modelbuilder.failed_tests += 1
		else
			# no error, check with res file, if any.
			var mmodule = test_method.mclassdef.mmodule
			var file = mmodule.filepath
			if file != null then
				var tries = [ file.dirname / mmodule.name + ".sav" / test_method.name + ".res",
					file.dirname / "sav" / test_method.name + ".res" ,
					file.dirname / test_method.name + ".res" ]
				var savs = [ for t in tries do if t.file_exists then t ]
				if savs.length == 1 then
					var sav = savs.first
					toolcontext.info("Diff output with {sav}", 1)
					res = toolcontext.safe_exec("diff -u --label 'expected:{sav}' --label 'got:{res_name}.out1' '{sav}' '{res_name}.out1' > '{res_name}.diff' 2>&1 </dev/null")
					if res == 0 then
						# OK
					else if toolcontext.opt_autosav.value then
						raw_output.write_to_file(sav)
						info = "Expected output updated: {sav} (--autoupdate)"
					else
						self.raw_output = "Diff\n" + "{res_name}.diff".to_path.read_all
						error = "Difference with expected output: diff -u {sav} {res_name}.out1"
						toolcontext.modelbuilder.failed_tests += 1
					end
				else if savs.length > 1 then
					toolcontext.info("Conflicting diffs: {savs.join(", ")}", 1)
					error = "Conflicting expected output: {savs.join(", ", " and ")} all exist"
					toolcontext.modelbuilder.failed_tests += 1
				else if not raw_output.is_empty then
					toolcontext.info("No diff: {tries.join(", ", " or ")} not found", 1)
					if toolcontext.opt_autosav.value then
						var sav = tries.first
						sav.dirname.mkdir
						raw_output.write_to_file(sav)
						info = "Expected output saved: {sav} (--autoupdate)"
					end
				end
			end
		end
		is_done = true
	end

	# Make the test case fail without testing it
	#
	# Useful when the compilation or the before_test failed.
	fun fail(message: String) do
		is_done = true
		error = message
		toolcontext.modelbuilder.failed_tests += 1
	end

	redef fun xml_classname do
		var a = test_method.full_name.split("$")
		return "nitunit.{a[0]}.{a[1]}"
	end

	redef fun xml_name do
		var a = test_method.full_name.split("$")
		return a[2]
	end
end
src/testing/testing_suite.nit:348,1--468,3