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. [ ] Occurred in testClass at test3.t(20)
文章來源于領測軟件測試網 http://www.k11sc111.com/