All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cucumber.junit.platform.engine.TestWeightCalculator Maven / Gradle / Ivy

There is a newer version: 4.2.9
Show newest version
package io.cucumber.junit.platform.engine;

import java.math.BigDecimal;
import java.net.URI;
import java.util.List;
import net.thucydides.model.environment.SystemEnvironmentVariables;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.support.descriptor.ClasspathResourceSource;

import net.serenitybdd.cucumber.suiteslicing.TestStatistics;


class TestWeightCalculator {

    private static TestStatistics statistics;

    static int calculateWeight(TestDescriptor descriptor) {
        return getEstimatedTestDuration(descriptor).intValue();
    }

    private static BigDecimal getEstimatedTestDuration(TestDescriptor descriptor) {
        if (statistics == null) {
            statistics = TestStatistics.from(SystemEnvironmentVariables.currentEnvironmentVariables(),
                                             List.of(URI.create("classpath:" + getTopFeatureDirectory(descriptor))));
        }
        String featureName = descriptor.getParent().map(TestDescriptor::getDisplayName).orElseThrow();
        String scenarioName = descriptor.getDisplayName();
        return statistics.scenarioWeightFor(featureName, scenarioName);
    }

    private static String getTopFeatureDirectory(TestDescriptor descriptor) {
        ClasspathResourceSource resource = (ClasspathResourceSource) descriptor.getSource().orElseThrow();
        return resource.getClasspathResourceName().split("/")[0];
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy