简体   繁体   English

Jqwik:如何检查是否涵盖了所有可能性?

[英]Jqwik: How to check if all possibilities is covered?

How to check if all possibilities (cartesian product of arguments) is covered in summary by N properties?如何检查所有可能性(参数的笛卡尔积)是否包含在 N 属性的摘要中? Some of them can be tested few times by different properties.其中一些可以通过不同的属性进行几次测试。

There's no way in jqwik to assert exhaustive parameter generation across property methods.在 jqwik 中没有办法断言跨属性方法的详尽参数生成。 What you can do, though, is to check all property conditions in a single property method:但是,您可以做的是在单个属性方法中检查所有属性条件:

@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);
}

In this case, all 24 combinations will be executed.在这种情况下,将执行所有 24 种组合。

Mind that generation = GenerationMode.EXHAUSTIVE is only necessary if the size of the cartesian product exceeds a property's number of tries (1000 by default).请注意,仅当笛卡尔积的大小超过属性的尝试次数(默认为 1000)时, generation = GenerationMode.EXHAUSTIVE才是必需的。

I found a solution with AfterContainer, but I have a problem with disappearing Statistics after every Property.我找到了 AfterContainer 的解决方案,但我遇到了在每个属性之后消失统计信息的问题。

private static final StatisticsCollectorImpl CLASS_STATISTICS_COLLECTOR = new StatisticsCollectorImpl("allCases");

Actually I am using manually created StatisticsCollectorImpl and updating it additionally in every Property.实际上,我正在使用手动创建的 StatisticsCollectorImpl 并在每个属性中另外更新它。 So, in result, I have statistics per property and per class.所以,结果,我有每个属性和每个 class 的统计数据。

Statistics.collect(p1, p2, p3);
CLASS_STATISTICS_COLLECTOR.collect(p1, p2, p3);

Fake property allCasesGenerator generates cartesian product.伪属性 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);
}

In AfterContainer I am checking cases existing only once - only from allCasesGenerator.在 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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM