1. 新建測試工程
選擇在一個測試用例中測試多個方法,并為測試生成壓力測試:
2. 為測試 Step 添加變量:
右鍵選中 Test Step ,添加一個 Grooy Script Step ;添加一個變量 count 并設置初始值為 0
3. 添加一個隨機變量:
右鍵選中 Test Step ,添加一個 Grooy Script Step ;名稱為 rand ,用下面的代碼產生一個隨機值
view plaincopy to clipboardprint?
01.Random rand = new Random()
02.result = "test_" + rand.nextInt(100)
Random rand = new Random()
result = "test_" + rand.nextInt(100)
4. 使用動態參數:
打開 init 操作的 SOAP 請求,將輸入參數用上面步驟產生的隨機替換
view plaincopy to clipboardprint?
http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.test.jastar.com.cn">
${rand#result}
http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.test.jastar.com.cn">
${rand#result}
5. 按條件選擇執行操作:
同樣的再添加一個 Grooy Script Step ;名稱叫 ExecHandler ,用來條件控制用例的執行
view plaincopy to clipboardprint?
01.def countProps = testRunner.testCase.getTestStepByName("count")
02.def index = countProps.getPropertyValue("count")
03.if (index == "0") {
04. testRunner.runTestStepByName("init")
05.} else {
06. testRunner.runTestStepByName("run")
07.}
08.countProps.setPropertyValue("count", "1")
def countProps = testRunner.testCase.getTestStepByName("count")
def index = countProps.getPropertyValue("count")
if (index == "0") {
testRunner.runTestStepByName("init")
} else {
testRunner.runTestStepByName("run")
}
countProps.setPropertyValue("count", "1")
6. 執行測試:
打開 LoadTest , disable 掉 init 和 run(ExecHandler 控制 init 和 run 的執行 ) ,設置好參數, OK 可以開始跑了。正如你所預期的 init 方法只執行一次,而 run 將會按照你所設置的方式來執行。