控制循环

该示例说明了如何使用定制代码扩展测试执行,从而控制循环。提供了样本代码,以向您显示可如何控制测试中循环的行为来更好地分析和验证测试结果。

该示例使用通过 IBM 的交易应用程序执行的股票购买交易的记录。此处显示的概念可在其他应用程序的测试中使用。

测试通过使用登录标识的数据池替换,从股票购买交易的记录开始。

页面包装在由五次迭代组成的循环中,如下图中所示:

请注意,在测试的各种页面中,存在 3 个定制代码项(由绿色圆圈内的“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);
 					session.putAsset(currentAsset);  

 				// 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")) {
						session.putAsset(currentAsset);  

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

			return null;
	}

该代码使用测试的数据区域来确定与标记 failedYet 关联的用户定义值。如果 failedYettrue,那么 StopLoopCheck 将打破测试循环。


反馈