如何检查所有的可能性(笛卡尔论证积)是否包含在总结中的N个性质?其中有些可以通过不同的特性测试几次。
发布于 2022-03-18 13:05:53
在jqwik中,无法在属性方法之间断言详尽的参数生成。但是,您可以在一个属性方法中检查所有属性条件:
@Property(generation = GenerationMode.EXHAUSTIVE)
void fullCartesianProductProperty(
@ForAll @IntRange(min = 0, max = 1) int p1,
@ForAll @IntRange(min = 0, max = 3) int p2,
@ForAll @IntRange(min = 1, max = 3) int p3
) {
MyEnum result = toCheck(p1, p2, p3);
if (p1 > 0) {
// Will only be executed in 12 cases
assertThat(result)...;
}
if (p2 <= 2) {
// Will only be executed in 18 cases
assertThat(result)...;
}
if (p3 > 1) {
// Will only be executed in 16 cases
assertThat(result)...;
}
// Will be executed in all 24 cases
assertThat(result);
}在这种情况下,将执行所有24个组合。
请注意,只有当笛卡尔积的大小超过一个属性的尝试次数(默认情况下为1000次)时,generation = GenerationMode.EXHAUSTIVE才是必要的。
发布于 2022-03-21 15:16:27
我在AfterContainer中找到了一个解决方案,但在每一处房产之后,我都遇到了消失统计数据的问题。
private static final StatisticsCollectorImpl CLASS_STATISTICS_COLLECTOR = new StatisticsCollectorImpl("allCases");实际上,我正在使用手动创建的StatisticsCollectorImpl,并在每个属性中额外更新它。因此,我有每个属性和每个类的统计数据。
Statistics.collect(p1, p2, p3);
CLASS_STATISTICS_COLLECTOR.collect(p1, p2, p3);假属性allCasesGenerator生成笛卡尔产品。
@Property(generation = GenerationMode.EXHAUSTIVE)
void allCasesGenerator (
@ForAll @IntRange(min = 0, max = 1) int p1,
@ForAll @IntRange(min = 0, max = 3) int p2,
@ForAll @IntRange(min = 1, max = 3) int p3
) {
CLASS_STATISTICS_COLLECTOR.collect(p1, p2, p3);
}在AfterContainer中,我只从allCasesGenerator中检查一次存在的情况。
@AfterContainer
static void afterAll() {
final List<String> presentOnlyInAllCasesGeneratorProperty = CLASS_STATISTICS_COLLECTOR.statisticsEntries()
.stream()
.filter(entry -> entry.count() <= 1)
.map(StatisticsEntryImpl::name)
.collect(Collectors.toList());
assertThat(presentOnlyInAllCasesGeneratorProperty)
.isEmpty();
}https://stackoverflow.com/questions/71510030
复制相似问题