Concordion – verifyRows 命令
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 应用程序 –
| Step | 描述 |
|---|---|
| 1 | 创建一个名为concordion的项目,并在创建的项目的src文件夹下创建一个包com.tutorialspoint。 |
| 2 | 使用添加外部 JAR选项添加所需的 Concordion 库,如Concordion – First Application章节中所述。 |
| 3 | 在com.tutorialspoint包下创建 Java 类System。 |
| 4 | 创建夹具类SystemFixture下specs.tutorialspoint包。 |
| 5 | 建立规范的HTML System.html下specs.tutorialspoint包。 |
| 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 测试运行的输出。