Studio:Scripting Test Units

From STRIDE Wiki
(Redirected from Scripting Test Units)
Jump to: navigation, search

Scripting a Test Unit

In most Test Unit applications, a standard, S2-supplied host test runner is sufficient. However, for applications that require finer control, Test Units may be run from custom-written scripts.

This capability is offered by the STRIDE Studio product.

Custom scripts can be written by hand or automatically generated using the Script Wizard and a corresponding template script. These scripts use the AutoScript (Ascript) COM object to access test metadata and features of the STRIDE Runtime. Specifically, the AutoScript TestUnits collection is used to represent the currently-available Test Units. The collection comprises Ascript TestUnit objects which represent the individual Test Units.

Scripting your Test Unit runner gives you the following extra capabilities:

  • You can pass values to C++ Class Test Unit constructors
  • You can programmatically control which test units run and their order
  • You can customize reporting options through the Reporter object

Single test unit example

The following example script is used to harness a test unit that has been captured using #pragma scl_test_class(Simple).

JavaScript

var tu = ascript.TestUnits.Item("Simple");
// Ensure test unit exists
if (tu != null) 
  tu.Run();

Perl

use strict;
use Win32::OLE;
Win32::OLE->Option(Warn => 3);
   
my $tu = $main::ascript->TestUnits->Item("Simple");
if (defined $tu) {
  $tu->Run();
}

Multiple test units example

The following example script is used to harness two test units that have been captured using #pragma scl_test_class(Simple1) and #pragma scl_test_class(Simple2).

JavaScript

var Units = ["Simple1","Simple2"];
  
// iterate through each function
for (i in Units)
{
  var tu = ascript.TestUnits.Item(Units[i]);
  if ( tu != null ) 
    tu.Run();
}

Perl

use strict;
use Win32::OLE;
Win32::OLE->Option(Warn => 3);
   
# initialize an array with all selected function names
my @UnitNames = ("Simple1","Simple2");
foreach (@UnitNames) {   
  my $tu = $main::ascript->TestUnits->Item($_->[1]);
  die "TestUnit not found: $_->[1]\n" unless (defined $tu);
  $tu->Run();
}