【发布时间】:2019-10-18 08:47:21
【问题描述】:
假设有一个函数可以确定按钮是否应该可见。
fun isButtonVisible(fitlers: List<Filters>, results: List<Shop>, isLoading: Boolean) {
return fitlers.isNotEmpty() && results.isEmpty() && !isLoading
}
现在我想用 PBT 来测试这个功能,比如:
"the button should be visible if filters is not empty and results is empty and is not loading" {
forAll { filters: List<Filters>, results: List<Shop>, isLoading: Boolean ->
val actual = isButtonVisible(filters, results, isLoading)
// Here reimplement the logic
val expected = filters.isNotEmpty() && results.isEmpty() && !isLoading
assertThat(actual).isEqual(expected)
}
}
看来我只是在我的测试中再次重新实现了逻辑,这是正确的吗?如果不是,如果逻辑只是几个标志的简单组合,我怎么能想出另一个属性?
【问题讨论】:
-
你可以重新实现,但我不建议这样做。相反,您应该计算出结果并对其进行硬编码。所以
assertThat(actual).isEqual(true)如果它应该是真的。 -
你能举个例子吗?在这种情况下,除了重新实现它之外,我不知道我可以使用哪些其他属性。
-
我已经举了一个例子。我不确定你在问什么。
-
"计算结果并对其进行硬编码"
-
这比重新实现要好,因为在某些情况下实现非常复杂,因此重新实现可能会出现错误。您不需要 8 种不同的测试;查看参数化测试。
标签: unit-testing testing kotlin property-based-testing