Studio:Using Scripts to Automate Software Testing: Difference between revisions
(New page: Much of what software developers generally dislike about testing can be overcome with better automation. Not only does automation reduce repetition, it will also help to ensure the accurac...) |
No edit summary |
||
Line 8: | Line 8: | ||
<source lang="cpp"> | |||
/* | |||
The Gregorian calendar has 97 leap years every 400 years: | |||
Every year divisible by 4 is a leap year, except:. | |||
- a year divisible by 100 is not a leap year except: | |||
- a year divisible by 400 is a leap year after all. | |||
For simplicity, any year prior to the introduction of the Gregorian Calendar | |||
(1582) is considered to be NOT a leap year. | |||
*/ | |||
#ifndef _LEAPYEAR_H_ | |||
#define _LEAPYEAR_H_ | |||
// first year of the gregorian calendar | |||
#define FIRST_GREGORIAN_YEAR 1582 | |||
typedef enum | |||
{ | |||
NO, | |||
YES, | |||
} eIsLeapYear; | |||
/** | |||
* Tests whether the passed-in year is a leap year | |||
* | |||
* @param nYear The year to be tested. Valid values are from | |||
* FIRST_GREGORIAN_YEAR to 0xffffffff inclusive | |||
* | |||
* @return YES - the passed-in year is a leap year | |||
* NO - the passed-in year is not a leap year, or is pre-gregorian | |||
*/ | |||
eIsLeapYear IsLeapYear(unsigned int uYear); | |||
#ifdef _SCL | |||
// tell the STRIDE compiler that the IsLeapYear should be captured (i.e. instrumented) | |||
#pragma scl_function(IsLeapYear) | |||
// tell the STRIDE compiler that the return value of IsLeapYear is constrained to | |||
// the enumeration eIsLeapYear | |||
#pragma scl_values(IsLeapYear(), eIsLeapYear) | |||
#endif | |||
#endif // _LEAPYEAR_H_ | |||
</source> | |||
<source lang="javascript"> | |||
// create a new test in the parent suite | |||
var test = testSuite.Tests.Add("Leap Year Test"); | |||
// initialize the test input values | |||
var arInput = new Array(1600, 1604, 1996, 2000); | |||
// get the function user instance | |||
var f = ascript.Functions.Item("IsLeapYear").User; | |||
var nIndex; | |||
// iterate through the members of arInput | |||
for (nIndex in arInput) { | |||
// set the parameter value | |||
f.ParameterList.uYear = arInput[nIndex]; | |||
// call the function synchronously; the STRIDE runtime will route the call to the | |||
// currently registered function owner | |||
f.Call(); | |||
// test the return value; note that the value is returned as the name of the enumerator | |||
if (f.ReturnValue == "YES") { | |||
test.Status = "PASS"; | |||
} | |||
else { | |||
test.Status = "FAIL"; | |||
break; | |||
} | |||
} | |||
</source> | |||
<source lang="perl"> | <source lang="perl"> | ||
use strict; | |||
use Win32::OLE; | |||
Win32::OLE->Option(Warn => 3); | |||
my $test = $main::testSuite->Tests->Add("Leap Year Positive Test"); | |||
my @input = (1600, 1604, 1996, 2000); | |||
my $f = $main::ascript->Functions->Item("IsLeapYear")->User; | |||
for my $i (0..$#input) | |||
{ | |||
$f->ParameterList->{uYear} = $input[$i]; | |||
$f->Call(); | |||
if ($f->ReturnValue eq "YES") { | |||
$test->{Status} = "PASS"; | |||
} | |||
else { | |||
$test->{Status} = "FAIL"; | |||
last; | |||
} | |||
} | |||
</source> | |||
[[Category:Scripting]] | [[Category:Scripting]] |
Revision as of 01:29, 5 March 2008
Much of what software developers generally dislike about testing can be overcome with better automation. Not only does automation reduce repetition, it will also help to ensure the accuracy and repeatability of tests. Because STRIDE makes your embedded application's interfaces accessible in the scripting language of your choice, using scripts is an ideal way to better automate testing.
The subject of this topic defines how to use scripts to supply inputs to a function, component, or application over one or more interfaces. These interfaces can be messaging-based or function-based. In STRIDE terminology, the script tends to be the User of the interface or interfaces. (In contrast, Using Scripts to Simulate Missing Software Units tends to involve scripts that are the Owner of an interface or interfaces.)
Via its objects, methods, and properties, ascript is the bridge between the interfaces in your embedded application and your favorite scripting language. A complete list of the objects, methods, and properties for use in your scripts can be found in the Online Help installed with STRIDE Studio. The objective of this topic is to focus on those objects, methods, and properties you will need to automate the testing of your software.
Note: To test a function, component, or application using a script, the corresponding interface(s) must be [accessible to STRIDE]. In addition, you will need an [intercept module] if any of the interfaces cross platform boundaries.
/*
The Gregorian calendar has 97 leap years every 400 years:
Every year divisible by 4 is a leap year, except:.
- a year divisible by 100 is not a leap year except:
- a year divisible by 400 is a leap year after all.
For simplicity, any year prior to the introduction of the Gregorian Calendar
(1582) is considered to be NOT a leap year.
*/
#ifndef _LEAPYEAR_H_
#define _LEAPYEAR_H_
// first year of the gregorian calendar
#define FIRST_GREGORIAN_YEAR 1582
typedef enum
{
NO,
YES,
} eIsLeapYear;
/**
* Tests whether the passed-in year is a leap year
*
* @param nYear The year to be tested. Valid values are from
* FIRST_GREGORIAN_YEAR to 0xffffffff inclusive
*
* @return YES - the passed-in year is a leap year
* NO - the passed-in year is not a leap year, or is pre-gregorian
*/
eIsLeapYear IsLeapYear(unsigned int uYear);
#ifdef _SCL
// tell the STRIDE compiler that the IsLeapYear should be captured (i.e. instrumented)
#pragma scl_function(IsLeapYear)
// tell the STRIDE compiler that the return value of IsLeapYear is constrained to
// the enumeration eIsLeapYear
#pragma scl_values(IsLeapYear(), eIsLeapYear)
#endif
#endif // _LEAPYEAR_H_
// create a new test in the parent suite
var test = testSuite.Tests.Add("Leap Year Test");
// initialize the test input values
var arInput = new Array(1600, 1604, 1996, 2000);
// get the function user instance
var f = ascript.Functions.Item("IsLeapYear").User;
var nIndex;
// iterate through the members of arInput
for (nIndex in arInput) {
// set the parameter value
f.ParameterList.uYear = arInput[nIndex];
// call the function synchronously; the STRIDE runtime will route the call to the
// currently registered function owner
f.Call();
// test the return value; note that the value is returned as the name of the enumerator
if (f.ReturnValue == "YES") {
test.Status = "PASS";
}
else {
test.Status = "FAIL";
break;
}
}
use strict;
use Win32::OLE;
Win32::OLE->Option(Warn => 3);
my $test = $main::testSuite->Tests->Add("Leap Year Positive Test");
my @input = (1600, 1604, 1996, 2000);
my $f = $main::ascript->Functions->Item("IsLeapYear")->User;
for my $i (0..$#input)
{
$f->ParameterList->{uYear} = $input[$i];
$f->Call();
if ($f->ReturnValue eq "YES") {
$test->{Status} = "PASS";
}
else {
$test->{Status} = "FAIL";
last;
}
}