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 Details

    • UnitTestModule

      public UnitTestModule()
  • Method Details

    • initialize

      public void initialize​(IScriptEngine engine, IEnvironment environment)
      Description copied from interface: IScriptModule
      Provides script engine and environment instances.
      Specified by:
      initialize in interface IScriptModule
      Overrides:
      initialize in class AbstractScriptModule
      Parameters:
      engine - script engine this module is loaded in
      environment - environment module that tracks this module
    • startTest

      public final void startTest​(String title, @ScriptParameter(defaultValue="") String description)
      Start a specific unit test. Started tests should be terminated by an {module #endTest()}.
      Parameters:
      title - name of test
      description - short test description
    • endTest

      public final void endTest()
      End the current test. Does nothing if no test was started.
    • getTestSuite

      public ITestSuite getTestSuite()
      Get the current test suite.
      Returns:
      test suite instance
    • getTestFile

      public ITestFile 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

      public void addMetaData​(String name, Object value)
      Append generic data to the current test, testfile or test suite.
      Parameters:
      name - key to use. Has to be unique for this test object
      value - 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 result
      actual - actual result
      errorDescription - 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 match
      candidate - text to be matched
      errorMessage - 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 result
      actual - actual result
      errorDescription - 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 not null.
      Parameters:
      actual - value to verify
      errorDescription - 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 is null.
      Parameters:
      actual - value to verify
      errorDescription - 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 is false.
      Parameters:
      actual - value to verify
      errorDescription - 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 is true.
      Parameters:
      actual - value to verify
      errorDescription - 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

      public final void assertion​(IAssertion reason) throws AssertionException
      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

      public ITestContainer addTestClass​(String className)
      Adds a new Testclass to the list of ITestFile in the Test. If testClass already exists, then it returns the first testNode with given className.
      Parameters:
      className - Name of testClass
      Returns:
      ITestContainer
    • ignore

      public void ignore​(@ScriptParameter(defaultValue="") String reason)
      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

      public void failure​(String message)
      Force a failure (=assertion) for the current test entity (test/testclass/testfile/testsuite).
      Parameters:
      message - failure message
    • failure

      public void failure​(String message, ScriptStackTrace stackTrace)
      Called from the javascript runner.
      Parameters:
      message - failure message
      stackTrace - stacktrace of failure event
    • getTest

      public ITest getTest()
      Get the current unit test.
      Returns:
      the current test or a generic global test scope if called outside of a valid testcase
    • error

      public void error​(String message) throws AssertionException
      Force an error for the current test entity (test/testclass/testfile/testsuite).
      Parameters:
      message - error message
      Throws:
      AssertionException - containing the provided message
    • error

      public void error​(String message, ScriptStackTrace stackTrace)
      Called from the javascript runner.
      Parameters:
      message - error message
      stackTrace - 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

      public Object executeUserCode​(String location) throws Exception
      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

      public static String[] 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.CoreException
      Create a test report file.
      Parameters:
      reportType - type of report; see getReportTypes() for values
      suite - ITestEntity to be reported
      fileLocation - location where report should be stored
      title - report title
      description - 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 file
      IOException - when we could not write to the file system
    • loadTestSuiteDefinition

      public ITestSuiteDefinition loadTestSuiteDefinition​(Object location) throws IOException
      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.CoreException
      Save a test suite definition to a file.
      Parameters:
      testsuite - definition to save
      fileLocation - location to save file to (file system or workspace)
      Throws:
      org.eclipse.core.runtime.CoreException - when we could not write to a workspace file
      IOException - when we could not write to the file system