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 将执行搜索函数,该函数应返回具有可预测迭代顺序的 Iterable 对象(例如 List、LinkedHashSet 或 TreeSet),verifyRows 对集合中的每个项目运行并运行 assertEquals 命令。

示例

让我们准备好一个可以运行的 Eclipse IDE,然后按照下面给出的步骤创建一个 Concordion 应用程序 −

步骤 描述
1 创建一个名为 concordion 的项目,并在所创建项目的 src 文件夹下创建一个包 com.tutorialspoint
2 使用 Add External JARs 选项添加所需的 Concordion 库,如 Concordion - First Application 中所述章。
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	utorialspoint\System.html
Successes: 2, Failures: 0

System.html 是 Concordion 测试运行的输出。

concordion verifyRows 输出