Silverlight支持ScriptableType 和 ScriptableMember属性,可以让类和成员被外部访问。因此可以用TestComplete进行调用。
automatedQA网站上的这篇文章介绍了具体的测试方法:
http://www.automatedqa.com/techpapers/testcomplete/automated-unit-testing-of-silverlight-applications/
单元测试还是在Silverlight程序中创建的,例如:
namespace nameExtractor
{
public class UnitTestDriver
{
//an object of the tested class nameExtractor
private nameExtractor nameExtractor;
public UnitTestDriver()
{
nameExtractor = new nameExtractor();
}
private voID CheckEquals(String Expected,String Actual,String Msg)
{
if (Expected != Actual)
{
String exceptionMsg = String.Format("{0} : expected {1},but was: {2}",Msg,Expected,Actual);
Exception exception = new Exception(exceptionMsg);
throw exception;
}
}
}
}
//tests
public voID Test1()
{
nameExtractor.Fullname = "Mr John Greench brown,PhD";
CheckEquals("Mr",nameExtractor.Title,"Title is not correct");
CheckEquals("John",nameExtractor.Firstname,"First name is not correct");
CheckEquals("Greench",nameExtractor.MIDdlename,"MIDdle name is not correct");
CheckEquals("brown",nameExtractor.Lastname,"Last name is not correct");
CheckEquals("PhD",nameExtractor.Suffix,"Suffix is not correct");
}
public voID Test2()
{
nameExtractor.Fullname = "John brown";
CheckEquals(""," Title is not correct");
CheckEquals("John"," First name is not correct");
CheckEquals(""," MIDdle name is not correct");
CheckEquals("brown"," Last name is not correct");
CheckEquals(""," Suffix is not correct");
nameExtractor.Fullname = "Mr. John brown";
CheckEquals("Mr"," Suffix is not correct");
nameExtractor.Fullname = "John brown,PhD";
CheckEquals("John"," First name is not correct");
CheckEquals("brown"," Last name is not correct");
CheckEquals("PhD"," Suffix is not correct");
}
为了让这些单元测试代码能够被TC所调用,需要做一些额外的准备:
private voID Application_Startup(object sender,StartupEventArgs e)
{
this.RootVisual = new MainPage();
HTMLPage.RegisterScriptableObject("unitTestDriver",new UnitTestDriver());
}
添加ScriptableType和ScriptaleMember属性:
using namespace System.windows.browser;
namespace nameExtractor
{
[ScriptableType] //ProvIDes scriptable access to the entire class
public class UnitTestDriver
{
…
}
}
using namespace System.windows.browser;
namespace nameExtractor
{
public class UnitTestDriver
{
…
//tests
[ScriptableMember] //ProvIDes scriptable access to Test1
public voID Test1()
{
…
}
[ScriptableMember] //ProvIDes scriptable access to Test2
public voID Test2()
{
…
}
}
}
接下来就可以在TC的脚本中调用Silverlight的单元测试代码:
// Jscript
function GetSlObject()
{
return Sys.Process("IEXPLORE",2).Page("file:///C:/MyProjects/nameExtractor/Bin/DeBUG/TestPage.HTML").Form("form1").Panel("silverlightControlHost").Object(0);
}
function Main()
{
var slObject = GetSlObject();var unitTestDriver = slObject.Content.unitTestDriver;Test1(unitTestDriver);Test2(unitTestDriver);}function Test1(unitTestDriver){try{unitTestDriver.Test1();Log.Message("Test1 passed.");}catch(ex){Log.Error("Test1 Failed.",ex.description);}}function Test2(unitTestDriver){try{unitTestDriver.Test2();Log.Message("Test2 passed.");}catch(ex){Log.Error("Test2 Failed.",ex.description);}}
总结以上是内存溢出为你收集整理的用TestComplete进行Silverlight的单元测试全部内容,希望文章能够帮你解决用TestComplete进行Silverlight的单元测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)