# Class goog.testing.TestCase

code »

A class representing a JsUnit test case. A TestCase is made up of a number of test functions which can be run. Individual test cases can override the following functions to set up their test environment: - runTests - completely override the test's runner - setUpPage - called before any of the test functions are run - tearDownPage - called after all tests are finished - setUp - called before each of the test functions - tearDown - called after each of the test functions - shouldRunTests - called before a test run, all tests are skipped if it returns false. Can be used to disable tests on browsers where they aren't expected to pass. Use `#autoDiscoverLifecycle` and `#autoDiscoverTests`

## Constructor

goog.testing.TestCase ( opt_name )

Parameters
opt_name: `string=` The name of the test case, defaults to 'Untitled Test Case'.

## Classes

goog.testing.TestCase.Error A class representing an error thrown by the test goog.testing.TestCase.Result A class for representing test results. goog.testing.TestCase.Test A class representing a single test function. goog.testing.TestCase.protectedDate_ No Description.

## Enumerations

goog.testing.TestCase.Order The order to run the auto-discovered tests.

Show:Public Protected Private

## Instance Methods

code »add ( test )

Adds a new test to the test case.

Parameters
test: `goog.testing.TestCase.Test` The test to add.

code »addNewTest ( name, ref, opt_scope )

Creates and adds a new test. Convenience function to make syntax less awkward when not using automatic test discovery.

Parameters
name: `string` The test name. ref: `!Function` Reference to the test function. opt_scope: `!Object=` Optional scope that the test function should be called in.

code »autoDiscoverLifecycle ( )

Adds any functions defined in the global scope that correspond to lifecycle events for the test case. Overrides setUp, tearDown, setUpPage, tearDownPage and runTests if they are defined.

code »autoDiscoverTests ( )

Adds any functions defined in the global scope that are prefixed with "test" to the test case.

code »clearTimeout ( id )

Clears a timeout created by `this.timeout()`.

Parameters
id: `number` A timeout id.

code »countNumFilesLoaded_ ( ) ⇒ `number`

Counts the number of files that were loaded for dependencies that are required to run the test.

Returns
The number of files loaded.

code »createTestFromAutoDiscoveredFunction ( name, ref ) ⇒ `!goog.testing.TestCase.Test`

Creates a `goog.testing.TestCase.Test` from an auto-discovered function.

Parameters
name: `string` The name of the function. ref: `function(): void` The auto-discovered function.
Returns
The newly created test.

code »cycleTests ( )

Cycles through the tests, breaking out using a setTimeout if the execution time has execeeded `#maxRunTime`.

code »doError ( test, opt_e )

Handles a test that failed.

Parameters
test: `goog.testing.TestCase.Test` The test that failed. opt_e: `*=` The exception object associated with the failure or a string.

code »doSuccess ( test )

Handles a test that passed.

Parameters
test: `goog.testing.TestCase.Test` The test that passed.

code »execute ( )

Executes each of the tests.

code »finalize ( )

Finalizes the test case, called when the tests have finished executing.

code »getActuallyRunCount ( ) ⇒ `number`

Returns the number of tests actually run in the test case, i.e. subtracting any which are skipped.

Returns
The number of un-ignored tests.

code »getAutoDiscoveryPrefix ( ) ⇒ `string`

Returns
The function name prefix used to auto-discover tests.

code »getBatchTime ( ) ⇒ `number`

Returns
Time since the last batch of tests was started.

code »getCount ( ) ⇒ `number`

Returns the number of tests contained in the test case.

Returns
The number of tests.

code »getGlobals ( opt_prefix ) ⇒ `!Array`

Gets list of objects that potentially contain test cases. For IE 8 and below, this is the global "this" (for properties set directly on the global this or window) and the RuntimeObject (for global variables and functions). For all other browsers, the array simply contains the global this.

Parameters
opt_prefix: `string=` An optional prefix. If specified, only get things under this prefix. Note that the prefix is only honored in IE, since it supports the RuntimeObject: http://msdn.microsoft.com/en-us/library/ff521039%28VS.85%29.aspx TODO: Remove this option.
Returns
A list of objects that should be inspected.

code »getName ( ) ⇒ `string`

Returns
The name of the test.

code »getNumFilesLoaded ( ) ⇒ `number`

Returns the number of script files that were loaded in order to run the test.

Returns
The number of script files.

code »getReport ( opt_verbose ) ⇒ `string`

Returns a string detailing the results from the test.

Parameters
opt_verbose: `boolean=` If true results will include data about all tests, not just what failed.
Returns
The results from the test.

code »getRunTime ( ) ⇒ `number`

Returns the amount of time it took for the test to run.

Returns
The run time, in milliseconds.

code »getTestResults ( ) ⇒ `!Object.<string>`

Returns the test results object: a map from test names to a list of test failures (if any exist).

Returns
Tests results object.

code »getTests ( ) ⇒ `!Array.<goog.testing.TestCase.Test>`

Gets the tests.

Returns
The test array.

code »getTimeStamp_ ( ) ⇒ `string`

Returns the current time.

Returns
HH:MM:SS.

code »isInsideMultiTestRunner ( ) ⇒ `boolean`

Returns
Whether the test case is running inside the multi test runner.

code »isSuccess ( ) ⇒ `boolean`

Returns
Whether the test was a success.

code »log ( val )

Logs an object to the console, if available.

Parameters
val: `*` The value to log. Will be ToString'd.

code »logError ( name, opt_e ) ⇒ `!goog.testing.TestCase.Error`

Parameters
name: `string` Failed test name. opt_e: `*=` The exception object associated with the failure or a string.
Returns
Error object.

code »maybeFailTestEarly ( testCase ) ⇒ `boolean`

Checks to see if the test should be marked as failed before it is run. If there was an error in setUpPage, we treat that as a failure for all tests and mark them all as having failed.

Parameters
testCase: `goog.testing.TestCase.Test` The current test case.
Returns
Whether the test was marked as failed.

code »next ( ) ⇒ `goog.testing.TestCase.Test`

Returns the current test and increments the pointer.

Returns
The current test case.

code »now ( ) ⇒ `number`

Returns
The current time in milliseconds, don't use goog.now as some tests override it.

code »orderTests_ ( tests )

Reorders the tests depending on the `order` field.

Parameters
tests: `Array.<goog.testing.TestCase.Test>` An array of tests to reorder.

code »pad_ ( number ) ⇒ `string`

Pads a number to make it have a leading zero if it's less than 10.

Parameters
number: `number` The number to pad.
Returns
The resulting string.

code »reset ( )

Resets the test case pointer, so that next returns the first test.

code »runTests ( )

Executes each of the tests. Overridable by the individual test case. This allows test cases to defer when the test is actually started. If overridden, finalize must be called by the test to indicate it has finished.

code »saveMessage ( message )

Saves a message to the result set.

Parameters
message: `string` The message to save.

code »setBatchTime ( batchTime )

Parameters
batchTime: `number` Time since the last batch of tests was started.

code »setCompletedCallback ( fn )

Sets the callback function that should be executed when the tests have completed.

Parameters
fn: `Function` The callback function.

code »setTests ( tests )

Sets the tests.

Parameters
tests: `!Array.<goog.testing.TestCase.Test>` A new test array.

code »setUp ( )

Gets called before every goog.testing.TestCase.Test is been executed. Can be overridden to add set up functionality to each test.

code »setUpPage ( )

Gets called before any tests are executed. Can be overridden to set up the environment for the whole test case.

code »shouldRunTests ( ) ⇒ `boolean`

Can be overridden in test classes to indicate whether the tests in a case should be run in that particular situation. For example, this could be used to stop tests running in a particular browser, where browser support for the class under test was absent.

Returns
Whether any of the tests in the case should be run.

code »tearDown ( )

Gets called after every goog.testing.TestCase.Test has been executed. Can be overriden to add tear down functionality to each test.

code »tearDownPage ( )

Gets called after all tests have been executed. Can be overridden to tear down the entire test case.

code »timeout ( fn, time ) ⇒ `number`

Calls a function after a delay, using the protected timeout.

Parameters
fn: `Function` The function to call. time: `number` Delay in milliseconds.
Returns
The timeout id.

code »trimPath_ ( path ) ⇒ `string`

Trims a path to be only that after google3.

Parameters
path: `string` The path to trim.
Returns
The resulting string.

## Instance Properties

code »batchTime_ : `number`

Time since the last batch of tests was started, if batchTime exceeds `#maxRunTime` a timeout will be used to stop the tests blocking the browser and a new batch will be started.

code »currentTestPointer_ : `number`

Pointer to the current test.

code »exceptionBeforeTest : `*`

Exception object that was detected before a test runs.

code »name_ : `string`

A name for the test case.

code »onCompleteCallback_ : `Function`

Optional callback that will be executed when the test has finalized.

code »order : `string`

The order to run the auto-discovered tests in.

code »result_ : `goog.testing.TestCase.Result`

Object used to encapsulate the test results.

code »running : `boolean`

Whether the test case is running.

code »startTime_ : `number`

Timestamp for when the test was started.

code »started : `boolean`

Whether the test case has ever tried to execute.

code »testsToRun_ : `Object`

Set of test names and/or indices to execute, or null if all tests should be executed. Indices are included to allow automation tools to run a subset of the tests without knowing the exact contents of the test file. Indices should only be used with SORTED ordering. Example valid values:

- [testName]
- [testName1, testName2]
- [2] - will run the 3rd test in the order specified
- [1,3,5]
- [testName1, testName2, 3, 5] - will work

code »tests_ : `!Array`

Array of test functions that can be executed.

## Static Functions

code »goog.testing.TestCase.getGlobals ( opt_prefix ) ⇒ `!Array`

Gets list of objects that potentially contain test cases. For IE 8 and below, this is the global "this" (for properties set directly on the global this or window) and the RuntimeObject (for global variables and functions). For all other browsers, the array simply contains the global this.

Parameters
opt_prefix: `string=` An optional prefix. If specified, only get things under this prefix. Note that the prefix is only honored in IE, since it supports the RuntimeObject: http://msdn.microsoft.com/en-us/library/ff521039%28VS.85%29.aspx TODO: Remove this option.
Returns
A list of objects that should be inspected.

code »goog.testing.TestCase.initializeTestRunner ( testCase )

Initializes the given test case with the global test runner 'G_testRunner'.

Parameters
testCase: `goog.testing.TestCase` The test case to install.

code »goog.testing.TestCase.protectedClearTimeout_ ( )

Save a reference to `window.clearTimeout`, so any code that overrides the default behavior (e.g. MockClock) doesn't affect our runner.

code »goog.testing.TestCase.protectedSetTimeout_ ( ) ⇒ `number`

Save a reference to `window.setTimeout`, so any code that overrides the default behavior (the MockClock, for example) doesn't affect our runner.

## Static Properties

code »goog.testing.TestCase.IS_IE : `boolean`

Avoid a dependency on goog.userAgent and keep our own reference of whether the browser is IE.

code »goog.testing.TestCase.currentTestName : `?string`

TODO(user) replace this with prototype.currentTest. Name of the current test that is running, or null if none is running.

code »goog.testing.TestCase.maxRunTime : `number`

The maximum amount of time that the test can run before we force it to be async. This prevents the test runner from blocking the browser and potentially hurting the Selenium test harness.

code »goog.testing.TestCase.setTimeoutAsString_ : `string`

Saved string referencing goog.global.setTimeout's string serialization. IE sometimes fails to uphold equality for setTimeout, but the string version stays the same.