SilkTest提供了一套自动恢复系统,让测试在出现异常和错误的情况下能够自动复原到指定的系统状态。在silktest入门快打系列里,我已经简单介绍了appstate的用法,这里也不再赘述。今天主要想讲一下其他一些可以与appstate配合使用的函数,以及他们之间的关系。
实际上,silktest的测试用例的组织,可以大致概括为plan包含script,script再包含case。也就是说有三层结构可以使用,当然在最上层,你还可以plan再包含plan,这种情况我们这里也不用讨论。一般来说plan调用script,然后script调用case,silktest对应地提供了TestPlanEnter(), TestPlanExit(),ScriptEnter(),ScriptExit(),TestCaseEnter(), TestCaseExit(),和Appstate()。Appstate()你可以将它看成为多个case之间运行的函数。所以他们之间的关系和顺序应该如下: TestPlanEnter()->ScriptEnter()->TestCaseEnter()->Appstate()->TestCaseExit()->ScriptExit()->TestPlanEnter(),看上去很像一个V字,对吧。他们的作用就是在进入或者退出plan,script,case的时候做一些初始化和清理工作。如果一个script中多个case都会用到的初始化,你就可以写在这个script的ScriptEnter()里面,都会用到的清理工作,你就可以放到ScriptExit()里面,对于其他的函数也是同理。 [-] Function Step1 [ ] script: FunctionStep1.t [ ] testcase: test1 [-] Function Step2 [ ] script: FunctionStep1.t [ ] testcase: test2 [-] Function Step3 [ ] script: FunctionStep2.t [ ] testcase: test3 [-] Function Step4 [ ] script: FunctionStep2.t [ ] testcase: test4 然后建立一个多个脚本共享的include文件FunctionStep.inc,内容如下: [-] TestPlanEnter() [ ] Print (“Enter Test plan”) [-] TestPlanExit(BOOLEAN bException) [ ] Print (“Leave Test plan”) [-] if(bException) [ ] ExceptLog () 最后有两个script,文件名分别是FunctionStep1.t和FunctionStep2.t,它们的内容如下: [ ] use “FunctionStep.inc” [ ] [-] ScriptEnter() [ ] Print(“Enter script1”) [-] ScriptExit(BOOLEAN bException) [ ] Print(“Leave script1”) [-] if (bException) [ ] ExceptLog () [ ] [-] TestCaseEnter() [ ] Print(“Enter Testcase”) [-] TestCaseExit (BOOLEAN bException) [ ] Print(“Leave Testcase”) [-] if (bException) [ ] ExceptLog () [ ] [-] testcase test1() [ ] print(“I’m in testcase1”) [-] testcase test2() [ ] print(“I’m in testcase2”) [ ] use “FunctionStep.inc” [-] appstate app_no() basedon DefaultBaseState [ ] print(“In Appstate”) [ ] [-] ScriptEnter() [ ] Print(“Enter script2”) [-] ScriptExit(BOOLEAN bException) [ ] Print(“Leave script2”) [-] if (bException) [ ] ExceptLog () [ ] [-] TestCaseEnter() [ ] Print(“Enter Testcase”) [ ] SetAppState () [-] TestCaseExit (BOOLEAN bException) [ ] Print(“Leave Testcase”) [-] if (bException) [ ] ExceptLog () [ ] [-] testcase test3() appstate app_no [ ] print(“I’m in testcase3”) [-] testcase test4() [ ] print(“I’m in testcase4”) 尝试着运行你的plan,你将会得到下面的结果: [-] Function Step1 [ ] Enter Test plan [ ] Enter script1 [ ] Enter Testcase [ ] I’m in testcase1 [ ] Leave Testcase [ ] Leave script1 [-] Function Step2 [ ] Enter script1 [ ] Enter Testcase [ ] I’m in testcase2 [ ] Leave Testcase [ ] Leave script1 [-] Function Step3 [ ] Enter script2 [ ] Enter Testcase [ ] *** DefaultBaseState is invoking Notepad [ ] In Appstate [ ] I’m in testcase3 [ ] Leave Testcase [ ] Leave script2 [-] Function Step4 [ ] Enter script2 [ ] Enter Testcase [ ] I’m in testcase4 [ ] Leave Testcase [ ] Leave script2 [ ] Leave Test plan 来分析一下,看看这些函数执行的顺序,是不是和咱讲的一样? |