Webtau提供了一些非常有表现力的快捷方式,比如TableData。我目前正在使用JUnit。例如,用于测试来自REST端点的预期数据,例如:
[
{ "uuid": "1-2-3-4", "directionShort": "N", "direction": "North" },
{ "uuid": "1-2-3-5", "directionShort": "S", "direction": "South" },
{ "uuid": "1-2-3-6", "directionShort": "E", "direction": "East" },
{ "uuid": "1-2-3-7", "directionShort": "W", "direction": "West" }
]
我可以这样写(是的,当然这个例子有点夸张):
import static org.assertj.core.api.Assertions.assertThat;
import static org.testingisdocumenting.webtau.WebTauDsl.http;
import static org.testingisdocumenting.webtau.Matchers.equal;
import static org.testingisdocumenting.webtau.WebTauCore.table;
import static org.testingisdocumenting.webtau.WebTauCore.____________;
http.get(URL_CARDINAL_POINTS,
(header, body) -> {
header.get("statusCode").shouldBe(equal(200));
assertThat(body.isList()).isTrue();
assertThat(body.numberOfElements()).isEqualTo(4);
body.should(equal(table(
"*directionShort", "direction",
____________,
"N", "North",
"S", "South",
"E", "East",
"W", "West"
)));
}
);
对于小的值集是可行的。对于较大的数据集,我想用containAll
进行测试,例如:
[
{ "uuid": "...", "name": "Stockholm", "country": "Sweden", "population": 10.42 },
{ "uuid": "...", "name": "Kiew", "country": "Ukraine", "population": 10.42 },
...
]
import static org.testingisdocumenting.webtau.Matchers.containAll;
http.get(URL_CAPITAL_CITIES,
(header, body) -> {
header.get("statusCode").shouldBe(equal(200));
assertThat(body.isList()).isTrue();
assertThat(body.numberOfElements()).isGreaterThan(10);
body.should(containAll(table(
"*name", "country", "population",
________________________________,
"Paris", "France", 2.161,
"Berlin", "Germany", 3.645,
"Kiew", "Ukraine", 2.884,
"Oslo", "Norway", 0.634,
"Madrid", "Spain", 3.223
)));
}
);
我试过了:
但这不起作用,它失败了:IllegalArgumentException: column 'name' is not present
。
省略键列标记时(name"前面的*
;列),错误变为:
body.should(containAll(table("name", "country", [...])
-->
"expecting body to contain all [<the table>]: no matches found for [<the table>]"
当使用contain()
代替containAll
时:
body.should(contain(table("name", "country", [...])
-->
"AssertionError: no match found"
丑陋的解决方案:
检查"manually"匹配:
Object dataNode;
dataNode = body.find(it -> "Paris".equals(it.get("name").get()));
assertThat(dataNode).isNotNull();
dataNode = body.find(it -> "Kiew".equals(it.get("name").get()));
assertThat(dataNode).isNotNull();
dataNode = body.find(it -> "Oslo".equals(it.get("name").get()));
assertThat(dataNode).isNotNull();
...
在试图比较除"键"以外的更多属性时,这是非常繁琐的。
仍然……丑陋的解决方案:
仍然与table
的表达性相距甚远,因为键使数据混乱:
import static org.testingisdocumenting.webtau.utils.CollectionUtils.map;
body.should(containAll(
map("name", "Paris", "country", "France", "population": 2.161),
map("name", "Berlin", "country", "Germany", "population": 3.645),
map("name", "Kiew", "country", "Ukraine", "population": 2.884),
map("name", "Oslo", "country", "Norway", "population": 0.634),
map("name", "Madrid", "country", "Spain", "population": 3.223)
));
问题:
是否有办法使用数据表table
而不是map
序列?
body.should(containAll(table(
"*name", "country", "population",
________________________________,
"Paris", "France", 2.161,
"Berlin", "Germany", 3.645,
"Kiew", "Ukraine", 2.884,
"Oslo", "Norway", 0.634,
"Madrid", "Spain", 3.223
)));
我认为这是一个很好的建议!我将不得不在WebTau中添加一条新规则以使其发挥作用。我理解你的意图,这是有道理的。
语法为
body.should(contain(table))
现在发布:https://testingisdocumenting.org/webtau/HTTP/matchers#contain-table
也非常高兴你使用table
!请随意加入不和谐服务器,我很想了解更多关于你的经验,想法和反馈。