Concordion - verifyRows 命令



Concordion verifyRows 命令可用於檢查系統返回的結果集合的內容。例如,如果我們在系統中設定了一組使用者並對它們進行部分搜尋,則系統應返回匹配的元素,否則我們的驗收測試應該失敗。

考慮以下需求:

<table>
   <tr><th>Users</th></tr>
   <tr><td>Robert De</td></tr>
   <tr><td>John Diere</td></tr>
   <tr><td>Julie Re</td></tr>
</table>

<p>Search for J should return:</p>

<table>
   <tr><th>Matching Users</th></tr>
   <tr><td>John Diere</td></tr>
   <tr><td>Julie Re</td></tr>
</table>

如果我們想為這樣的搜尋函式編寫規範,該函式將搜尋並返回一個集合,則規範如下:

<table concordion:execute = "addUser(#username)">
   <tr><th concordion:set = "#username">Username</th></tr>
   <tr><td>Robert De</td></tr>
   <tr><td>John Diere</td></tr>
   <tr><td>Julie Re</td></tr>
</table>

<p>Search for "<b concordion:set = "#searchString">J</b>" should return:</p>

<table concordion:verifyRows = "#username : search(#searchString)">
   <tr><th concordion:assertEquals = "#username">Matching Usernames</th></tr>
   <tr><td>John Diere</td></tr>
   <tr><td>Julie Re</td></tr>
</table>

當 Concordion 解析文件時,它將在第一個表格的每一行上執行 addUser(),然後將 searchString 設定為 J。接下來,Concordion 將執行搜尋函式,該函式應返回一個具有可預測迭代順序的可迭代物件(例如,List、LinkedHashSet 或 TreeSet),verifyRows 針對集合的每個專案執行並執行 assertEquals 命令。

示例

讓我們準備好一個可用的 Eclipse IDE,並按照以下步驟建立 Concordion 應用程式:

步驟 描述
1 建立一個名為 concordion 的專案,並在建立的專案中的 src 資料夾下建立一個包 com.tutorialspoint
2 使用 新增外部 JAR 選項新增所需的 Concordion 庫,如 Concordion - 第一個應用程式 章節中所述。
3 com.tutorialspoint 包下建立 Java 類 System
4 specs.tutorialspoint 包下建立 Fixture 類 SystemFixture
5 specs.tutorialspoint 包下建立規範 html System.html
6 最後一步是建立所有 Java 檔案和規範檔案的內容,並按如下所述執行應用程式。

以下是 System.java 檔案的內容:

package com.tutorialspoint;

import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class System { 
   private Set<String> users = new HashSet<String>();
	
   public void addUser(String username) {
      users.add(username);
   }
	
   public Iterable<String> search(String searchString) {
      SortedSet<String> matches = new TreeSet<String>();
		
      for (String username : users) {
         if (username.contains(searchString)) {
            matches.add(username);
         }
      }
		
      return matches;
   }
}

以下是 SystemFixture.java 檔案的內容:

package specs.tutorialspoint;

import org.concordion.integration.junit4.ConcordionRunner;
import org.junit.runner.RunWith;
import com.tutorialspoint.System;

@RunWith(ConcordionRunner.class)

public class SystemFixture {
   System system = new System();
   public void addUser(String username) {
      system.addUser(username);
   }
	
   public Iterable<String> search(String searchString) {
      return system.search(searchString);
   }
}

以下是 System.html 檔案的內容:

<html xmlns:concordion = "http://www.concordion.org/2007/concordion">
   <head>
      <link href = "../concordion.css" rel = "stylesheet" type = "text/css" />
   </head>

   <body>
      <h1>System Specifications</h1>
      <p>We are building specifications for our online order tracking application.</p>
      <p>Following is the requirement to add a partial search capability on user names:</p>
		
      <div class = "example">      
         <h3>Example</h3>
			
         <table concordion:execute = "addUser(#username)">
            <tr><th concordion:set = "#username">Username</th></tr>
            <tr><td>Robert De</td></tr>
            <tr><td>John Diere</td></tr>
            <tr><td>Julie Re</td></tr>
         </table>
			
         <p>Search for "<b concordion:set = "#searchString">J</b>" should return:</p>
			
         <table concordion:verifyRows = "#username : search(#searchString)">
            <tr><th concordion:assertEquals = "#username">Matching Usernames</th></tr>
            <tr><td>John Diere</td></tr>
            <tr><td>Julie Re</td></tr>
         </table>
			
      </div> 
		
   </body>

</html>

完成原始檔和規範檔案的建立後,讓我們以 JUnit 測試的方式執行應用程式。如果應用程式一切正常,它將產生以下結果:

C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\concordion\specs\tutorialspoint\System.html
Successes: 2, Failures: 0

System.html 是 Concordion 測試執行的輸出。

concordion verifyRows Output
廣告

© . All rights reserved.