Package org.eclipse.ease.lang.unittest
Class UnitTestModule
java.lang.Object
org.eclipse.ease.modules.AbstractScriptModule
org.eclipse.ease.lang.unittest.UnitTestModule
- All Implemented Interfaces:
IScriptModule
public class UnitTestModule extends AbstractScriptModule
Support methods for scripted unit tests. Provides several assertion methods and utility functions to manipulate the current test instances and states.
-
Constructor Summary
Constructors Constructor Description UnitTestModule()
-
Method Summary
Modifier and Type Method Description void
addMetaData(String name, Object value)
Append generic data to the current test, testfile or test suite.ITestContainer
addTestClass(String className)
Adds a new Testclass to the list ofITestFile
in the Test.static IAssertion
assertEquals(Object expected, Object actual, Object errorDescription)
Expect two objects to be equal.static IAssertion
assertFalse(Boolean actual, Object errorDescription)
Asserts when provided value istrue
.void
assertion(IAssertion reason)
Create a new assertion for the current test.static IAssertion
assertMatch(String pattern, String candidate, String errorMessage)
Asserts when provided value does not match to a given regular expression pattern.static IAssertion
assertNotEquals(Object expected, Object actual, Object errorDescription)
Expect two objects not to be equal.static IAssertion
assertNotNull(Object actual, Object errorDescription)
Asserts when provided value isnull
.static IAssertion
assertNull(Object actual, Object errorDescription)
Asserts when provided value is notnull
.static IAssertion
assertTrue(Boolean actual, Object errorDescription)
Asserts when provided value isfalse
.void
createReport(String reportType, ITestEntity suite, Object fileLocation, String title, String description, Object reportData)
Create a test report file.void
endTest()
End the current test.void
error(String message)
Force an error for the current test entity (test/testclass/testfile/testsuite).void
error(String message, ScriptStackTrace stackTrace)
Called from the javascript runner.Object
executeUserCode(String location)
Execute code registered in the testsuite.void
failure(String message)
Force a failure (=assertion) for the current test entity (test/testclass/testfile/testsuite).void
failure(String message, ScriptStackTrace stackTrace)
Called from the javascript runner.static String[]
getReportTypes()
Get a list of available test report types.ITest
getTest()
Get the current unit test.ITestFile
getTestFile()
Get the currently executed test file instance.ITestSuite
getTestSuite()
Get the current test suite.void
ignore(String reason)
Ignore the current test, the current testfile or test suite.void
initialize(IScriptEngine engine, IEnvironment environment)
Provides script engine and environment instances.ITestSuiteDefinition
loadTestSuiteDefinition(Object location)
Load a test suite definition from a given resource.void
saveTestSuiteDefinition(ITestSuiteDefinition testsuite, Object fileLocation)
Save a test suite definition to a file.void
setTestTimeout(long timeout)
Set the timeout for the current test.void
setThrowOnFailure(boolean throwOnFailure)
Changes behavior for assertion handling.void
startTest(String title, String description)
Start a specific unit test.
-
Constructor Details
-
UnitTestModule
public UnitTestModule()
-
-
Method Details
-
initialize
Description copied from interface:IScriptModule
Provides script engine and environment instances.- Specified by:
initialize
in interfaceIScriptModule
- Overrides:
initialize
in classAbstractScriptModule
- Parameters:
engine
- script engine this module is loaded inenvironment
- environment module that tracks this module
-
startTest
Start a specific unit test. Started tests should be terminated by an {module #endTest()}.- Parameters:
title
- name of testdescription
- short test description
-
endTest
public final void endTest()End the current test. Does nothing if no test was started. -
getTestSuite
Get the current test suite.- Returns:
- test suite instance
-
getTestFile
Get the currently executed test file instance. The test file is not a file instance but the runtime representation of a testsuite test file.- Returns:
- test file instance
-
addMetaData
Append generic data to the current test, testfile or test suite.- Parameters:
name
- key to use. Has to be unique for this test objectvalue
- data to be stored
-
assertEquals
public static IAssertion assertEquals(Object expected, Object actual, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object errorDescription)Expect two objects to be equal.- Parameters:
expected
- expected resultactual
- actual resulterrorDescription
- optional error text to be displayed when not equal- Returns:
- assertion containing comparison result
-
assertMatch
public static IAssertion assertMatch(String pattern, String candidate, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") String errorMessage)Asserts when provided value does not match to a given regular expression pattern.- Parameters:
pattern
- pattern to matchcandidate
- text to be matchederrorMessage
- error message in case of a mismatch- Returns:
- assertion depending on
actual
value
-
assertNotEquals
public static IAssertion assertNotEquals(Object expected, Object actual, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object errorDescription)Expect two objects not to be equal.- Parameters:
expected
- unexpected resultactual
- actual resulterrorDescription
- optional error text to be displayed when equal- Returns:
- assertion containing comparison result
-
assertNull
public static IAssertion assertNull(Object actual, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object errorDescription)Asserts when provided value is notnull
.- Parameters:
actual
- value to verifyerrorDescription
- optional error description- Returns:
- assertion depending on
actual
value
-
assertNotNull
public static IAssertion assertNotNull(Object actual, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object errorDescription)Asserts when provided value isnull
.- Parameters:
actual
- value to verifyerrorDescription
- optional error description- Returns:
- assertion depending on
actual
value
-
assertTrue
public static IAssertion assertTrue(Boolean actual, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object errorDescription)Asserts when provided value isfalse
.- Parameters:
actual
- value to verifyerrorDescription
- optional error description- Returns:
- assertion depending on
actual
value
-
assertFalse
public static IAssertion assertFalse(Boolean actual, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object errorDescription)Asserts when provided value istrue
.- Parameters:
actual
- value to verifyerrorDescription
- optional error description- Returns:
- assertion depending on
actual
value
-
setThrowOnFailure
public void setThrowOnFailure(boolean throwOnFailure)Changes behavior for assertion handling. By default assertions do not cause an exception to be thrown, which supports classic (functional) test mode. Object oriented test mode enhances exceptions to be thrown on assertions.- Parameters:
throwOnFailure
-true
to thow exceptions on assertions
-
assertion
Create a new assertion for the current test. According to the assertion status an error might be added to the current testcase.- Parameters:
reason
- assertion to be checked- Throws:
AssertionException
- in case is enabled
-
addTestClass
Adds a new Testclass to the list ofITestFile
in the Test. If testClass already exists, then it returns the first testNode with given className.- Parameters:
className
- Name of testClass- Returns:
ITestContainer
-
ignore
Ignore the current test, the current testfile or test suite. What is ignored depends on the scope this command is executed in.- Parameters:
reason
- message why the test got ignored.
-
failure
Force a failure (=assertion) for the current test entity (test/testclass/testfile/testsuite).- Parameters:
message
- failure message
-
failure
Called from the javascript runner.- Parameters:
message
- failure messagestackTrace
- stacktrace of failure event
-
getTest
Get the current unit test.- Returns:
- the current test or a generic global test scope if called outside of a valid testcase
-
error
Force an error for the current test entity (test/testclass/testfile/testsuite).- Parameters:
message
- error message- Throws:
AssertionException
- containing the provided message
-
error
Called from the javascript runner.- Parameters:
message
- error messagestackTrace
- stacktrace of error event
-
setTestTimeout
public void setTestTimeout(long timeout)Set the timeout for the current test. If test execution takes longer than the timeout, the test is marked as failed.- Parameters:
timeout
- timeout in [ms]
-
executeUserCode
Execute code registered in the testsuite.- Parameters:
location
- name of the code fragment to execute.- Returns:
- execution result
- Throws:
Exception
- when no user specific code can be found or the injected code throws
-
getReportTypes
Get a list of available test report types.- Returns:
- String array containing available report types
-
createReport
public void createReport(String reportType, ITestEntity suite, Object fileLocation, @ScriptParameter(defaultValue="Test Report") String title, @ScriptParameter(defaultValue="") String description, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object reportData) throws IOException, org.eclipse.core.runtime.CoreExceptionCreate a test report file.- Parameters:
reportType
- type of report; see getReportTypes() for valuessuite
-ITestEntity
to be reportedfileLocation
- location where report should be storedtitle
- report titledescription
- report description (ignored by some reports)reportData
- additional report data. Specific to report type- Throws:
org.eclipse.core.runtime.CoreException
- when we could not write to a workspace fileIOException
- when we could not write to the file system
-
loadTestSuiteDefinition
Load a test suite definition from a given resource.- Parameters:
location
- location to load from- Returns:
- test suite definition
- Throws:
IOException
- when reading of definition fails
-
saveTestSuiteDefinition
public void saveTestSuiteDefinition(ITestSuiteDefinition testsuite, Object fileLocation) throws IOException, org.eclipse.core.runtime.CoreExceptionSave a test suite definition to a file.- Parameters:
testsuite
- definition to savefileLocation
- location to save file to (file system or workspace)- Throws:
org.eclipse.core.runtime.CoreException
- when we could not write to a workspace fileIOException
- when we could not write to the file system
-