SilkTest天龙八部系列1-初始化和构造函数

发表于:2011-04-01来源:作者:点击数: 标签:
SilkTest天龙八部系列1-初始化和构造函数 软件测试 SilkTest没有提供专门的构造函数机制,但是在类对象生成的过程中,会先初始化在类中申明的变量。我们可以在初始化该变量的时, 调用某些函数完成对象初始化工作,看上去好像是调用了构造函数一样。不过要记

  SilkTest天龙八部系列1-初始化和构造函数 软件测试

  SilkTest没有提供专门的构造函数机制,但是在类对象生成的过程中,会先初始化在类中申明的变量。我们可以在初始化该变量的时,

  调用某些函数完成对象初始化工作,看上去好像是调用了构造函数一样。不过要记住的是,这只是用来模拟构造函数而已。

  下面是一个例子:view plaincopy to clipboardprint?01.[-] winclass myClass 02. [ ] boolean bConstructed = Constructor (SubStr (WindowTag(this), 2)) 03. [ ] string sSetMe 04. [ ] integer i 05. [ ] 06. [-] boolean Constructor (string sConstructorData) 07. [-] if (!IsSet(bConstructed)) 08. [ ] sSetMe = sConstructorData 09. [ ] return true 10. [ ] 11. [-] property SetMe 12. [-] string Get() 13. [ ] return sSetMe 14.[ ] 15.[ ] // test that the myClass constructor works 16.[-] testcase testClass () appstate none 17. [ ] window myObject = myClass ("Set property") 18. [ ] print(WindowTag(myObject)) 19. [ ] print ("myObject.SetMe = {myObject.SetMe}") 20. [ ] print ("myObject.i = {myObject.i}") [-] winclass myClass

  [ ] boolean bConstructed = Constructor (SubStr (WindowTag(this), 2))

  [ ] string sSetMe

  [ ] integer i

  [ ]

  [-] boolean Constructor (string sConstructorData)

  [-] if (!IsSet(bConstructed))

  [ ] sSetMe = sConstructorData

  [ ] return true

  [ ]

  [-] property SetMe

  [-] string Get()

  [ ] return sSetMe

  [ ]

  [ ] // test that the myClass constructor works

  [-] testcase testClass () appstate none

  [ ] window myObject = myClass ("Set property")

  [ ] print(WindowTag(myObject))

  [ ] print ("myObject.SetMe = {myObject.SetMe}")

  [ ] print ("myObject.i = {myObject.i}")

  我们来分析一下这段代码。

  一开始声明了一个myClass类,他有三个变量,其中bConstructed的初始化调用了类的一个成员函数Constructor(),这个函数其实

  可以叫任何名字。所以,window myObject = myClass ("Set property")这句直接会导致

  成员函数Constructor()被调用。那么,为什么要给该函数传一个参数SubStr (WindowTag(this), 2)进去呢,这需要了解一下

  WindowTag()的意义,WindowTag()会返回对象的tag,而不管该对象是否存在。一般的Tag都是以斜线/开头的,

  window myObject = myClass ("Set property")这句的意思你就可以理解为(其实这不太容易理解,没办法,4Test当初就是这么设计的)

  获取对象"Set property"的Tag,它的Tag就是"/Set property"。所以在初始化bConstructed的过程中,同时初始化了成员变量

  bConstructed,而成员变量i并没有被初始化,所以当你运行整个脚本的时候,会得到如下的输出:

  view plaincopy to clipboardprint?01.[-] Testcase testClass - 1 error 02. [ ] /Set property 03. [ ] myObject.SetMe = Set property 04. [ ] *** Error: Variable (i) has not been set 05. [ ] Oclearcase/" target="_blank" >ccurred in testClass at test3.t(20)

原文转自:http://www.ltesting.net