Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 16, 2022 11:00 pm GMT

JUnit Cmo repetir un test con parmetros

En publicaciones anteriores hemos visto de qu maneras se poda repetir la ejecucin de un test, as como la manera de parametrizar un test.
En esta ocasin vamos a ver cmo unir ambas ideas para repetir un test parametrizado.

Utilizaremos para ello el mismo ejemplo de la publicacin repetir la ejecucin de un test, recuerdo que se trata de un mtodo que genera un nmero aleatorio de N dgitos.

Inicialmente, puede que pensamos que es suficiente con combinar las anotaciones mencionadas en las otras publicaciones, @RepeatedTest + @ParameterizedTest, pero lamentablemente JUnit no soporta esta funcionalidad actualmente. Por fortuna la solucin alternativa es muy simple.

Supongamos que partimos de un test parametrizado:

/** * Parameters: * 1. int - The lower bound. * 2. int - The upper bound. * 3. int - The number of digits of the random generated value. */private static Stream<Arguments> randomNumberGeneratorParams() {    return Stream.of(        Arguments.of(1, 9, 1),        Arguments.of(10, 99, 2),        Arguments.of(100, 999, 3),        Arguments.of(1000, 9999, 4),        Arguments.of(10000, 99999, 5)    );}@ParameterizedTest@MethodSource(value = "randomNumberGeneratorParams")@DisplayName(value = "Random numbers generator")void random_numbers_generator(int low, int high, int numOfDigits) {        var actual = RandomUtil.generateNumber(numOfDigits);    assertTrue(high >= actual, "Error, random is too high");    assertTrue(low <= actual, "Error, actual is too low");}// Test passed: 5 of 5 // [1] low = 1, high = 9, numOfDigits = 1 -> numero generado 6// [2] low = 10, high = 99, numOfDigits = 2 -> nmero generado 56// [3] low = 100, high = 999, numOfDigits = 3 -> ...// [4] low = 1000, high = 9999, numOfDigits = 4// [5] low = 10000, high = 99999, numOfDigits = 5

Podemos imaginar que este test no es muy fiable, ya que slo hemos generado 1 nmero aleatorio para cada uno de nuestros casos de uso (generar un nmero de 1, 2, 3, 4 y 5 dgitos).

Para confirmar con seguridad que la funcin aleatoria es correcta, sera mejor si pudiramos generar N nmeros en cada rango o caso de uso, y comprobar que todos ellos cumplen con el test.

Veamos cmo hacerlo:

private static final int NUMBER_REPETITIONS = 1_000;/** * Parameters: * 1. int - The lower bound. * 2. int - The upper bound. * 3. int - The number of digits of the random generated value. */private static Stream<Arguments> randomNumberGeneratorParams() {    return Stream.of(        Arguments.of(1, 9, 1),        Arguments.of(10, 99, 2),        Arguments.of(100, 999, 3),        Arguments.of(1000, 9999, 4),        Arguments.of(10000, 99999, 5)    );}@ParameterizedTest@MethodSource(value = "randomNumberGeneratorParams")@DisplayName(value = "Random numbers generator")void random_numbers_generator(int low, int high, int numOfDigits) { int iterationCount = 1; do {   // <- Mediante el uso del loop do-while, podemos iterar la generacin de los nmeros aleatorios     var actual = RandomUtil.generateNumber(numOfDigits);     assertTrue(high >= actual, "Error, random is too high");     assertTrue(low <= actual, "Error, actual is too low");     iterationCount++; } while (iterationCount <= NUMBER_REPETITIONS);}// Test passed: 5 of 5 // [1] low = 1, high = 9, numOfDigits = 1 -> nmeros generados: 6, 3, 1, 3, 4, 2, 6, 8, 9...1_000_000 de nmeros generados// [2] low = 10, high = 99, numOfDigits = 2 -> ...// [3] low = 100, high = 999, numOfDigits = 3// [4] low = 1000, high = 9999, numOfDigits = 4// [5] low = 10000, high = 99999, numOfDigits = 5

De esta manera, el test generar tantos nmeros como indiquemos en NUMBER_REPETITIONS, y confirmar que todos ellos cumplen con las afirmaciones del test.


Original Link: https://dev.to/gekyzo/junit-como-repetir-un-test-con-parametros-1l15

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To