控制迴圈

這個範例示範如何利用自訂程式碼來控制迴圈,以延伸測試的執行。它提供範例程式碼,顯示如何在測試內操作迴圈行為,以進一步分析及驗證測試結果。

這個範例使用一份採用 IBM 交易應用程式的股票購買交易記錄。這裡顯示的概念可使用於其他應用程式的測試。

這項測試是從股票購買交易記錄開始,它在登入 ID 方面使用資料儲存區替代項。

如下圖所示,各頁面是包裝在含有五次反覆運算的迴圈中:

請注意,在測試的各個頁面中,會有自訂程式碼的三個項目(以其中出現 "C" 的綠色圓圈表示)。這個範例探索自訂程式碼的這些項目。

這裡提到自訂程式碼的第一個片段 InitializeBuyTest

package customcode;

import java.util.Random;

import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.services.IVirtualUserInfo;

/**
 * @author unknown
 */
public class InitializeBuyTest implements
		com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

	/**
	 * Instances of this will be created using the no-arg constructor.
	 */
		public InitializeBuyTest() {
	}

	/**
	 	 * For description of ICustomCode2 and ITestExecutionServices interfaces,
	 	 * see the Javadoc information. */
	public String exec(ITestExecutionServices tes, String[] args) {
				// Get the test's data area and set a flag indicating that nothing
				// has failed yet. This flag will be used later to break out
				// of the schedule loop as soon as a failure is encountered.
				IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
				dataArea.put("failedYet", "false");

				// Get the virtual users's data area
				IDataArea vda = tes.findDataArea(IDataArea.VIRTUALUSER);
		
				// Randomly select a stock to purchase from the set of s:0 to s:499.
	    	    IVirtualUserInfo vuInfo = (IVirtualUserInfo) vda.get(IVirtualUserInfo.KEY);
	    	    Random rand = vuInfo.getRandom();
				String stock = "s:" + Integer.toString(rand.nextInt(499));

				// Persist the name of the stock in the virtual user's data area.
				vda.put("myStock", stock);

				return stock;
	}

這個自訂程式碼是位在 exec() 方法中。

首先,會取得測試的資料區以儲存旗標值,以本例來說,為一個文字字串,以便在往後發現錯誤時用來停止測試迴圈。以這種方式儲存的資料可持續保存在各項測試中。

接著,會建立一個隨機產生的股票字串。此值會儲存成 stock 變數,並當成方法的回覆值傳回。如下圖所示,這個回覆值會在稍後作為要求中的一個替代項:

強調顯示的項目使用替代項 (s%3A716),它是 InitializeBuyTest 自訂程式碼項目傳回的值。 我們使用自訂程式碼來驅動測試的方向。

InitializeBuyTest 中接下來幾行的程式碼使用「虛擬使用者」資料區來儲存股票名稱,供未來參照。 同樣地,以這種方式儲存的資料可持續保存在各項測試中。

第二個自訂程式碼片段稱為 CheckStock。 其內容如下(此時只列出 exec() 方法):

public String exec(ITestExecutionServices tes, String[] args) {

				// Get the actual and requested stock purchased.
				String actualStock = args[0].replaceAll("<B>", "");
 				actualStock = actualStock.substring(0, actualStock.indexOf("<"));
				String requestedStock = args[1];

  				// Set the log level to ALL.
 				IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
 				ITestInfo testInfo = (ITestInfo)dataArea.get(ITestInfo.KEY);
 										button.press();

 				// If the log level is set to ALL, report the actual and requested stock
				// purchased.
 				ITestLogManager testLogManager = tes.getTestLogManager();
					if (testLogManager.wouldReport(ITestLogManager.ALL)) {
 						testLogManager.reportMessage("Actual stock purchased: "
 										+ actualStock + ". Requested stock: " + requestedStock
					+ ".");
 		}  		

				// If the actual and requested stock don't match, submit a FAIL verdict.
 					if (testLogManager.wouldReport(ITestLogManager.ALL)) {
						if (!actualStock.equalsIgnoreCase(requestedStock)) {
 				testLogManager.reportVerdict(
						"Actual and requested purchase stock do not match.",
 						VerdictEvent.VERDICT_FAIL);

 								// Use the test's data area to record the fact that an error has 			
								// occurred.
 								dataArea.put("failedYet", "true");
 			} 		
		}
  					return null;
	}

這個程式碼一開始會擷取兩個已傳給程式碼的引數。會強調顯示原始記錄中的一部分回應,並作為參照之用,如下圖所示。

需要進行一些字串操作,以取得相關的文字;在這個案例中,是實際購買的股票名稱。之後,便將這個新建的參照當作引數傳給 CheckStock,如下圖所示:

請注意,InitializeBuyTest 的回覆值也會當成引數傳入。

CheckStock 自訂程式碼項目利用這些值來確認在測試執行期間,實際購買了 InitializeBuyTest 所產生的隨機選擇股票。

接著,CheckStock 會設定測試日誌層次,報告實際和所要求的股票購買,如果不相符,則會產生「失敗」裁決。CheckStock 也會在測試的資料區中儲存一個與 failedYet 標籤相關聯的 true 值。

這裡提到自訂程式碼的第三個片段(僅 exec() 方法):

public String exec(ITestExecutionServices tes, String[] args) {

				// Get the test log manager.
				ITestLogManager testLogManager = tes.getTestLogManager();
		
				// Get the test's data area and get a flag indicating to
				// see if anything has failed yet. If so, stop the loop.
				IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
				String failedYet = (String) dataArea.get("failedYet");

				// Break out of the loop if an error has been encountered.
				if (failedYet.equalsIgnoreCase("true")) {
						tes.getLoopControl().breakLoop();

						if (testLogManager.wouldReport(ITestLogManager.ALL)) {
								testLogManager.reportMessage("Loop stopped.");
			}
		}

					return null;
	}

此程式碼利用測試的資料區來判斷與 failedYet 標籤相關聯的使用者定義值。 如果 failedYettrueStopLoopCheck 會跳出測試迴圈。


意見