Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Use Parameterized Tests. RSPEC-5976

Aperta
#203 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
30/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
java
Ambito
testing-qa

Direzione di ricerca

Non è indicato alcun file di implementazione né alcun percorso di test. Inizia esaminando le ricette di test Java esistenti nel repository e i relativi test, quindi confronta il comportamento previsto con la regola RSPEC-5976 collegata. Il lavoro è completato quando il framework è in grado di gestire i casi di test parametrizzati della regola e dispone della copertura per gli esempi descritti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

recipe

Is this something that could be achieved with this framework?

When multiple tests differ only by a few hardcoded values they should be refactored as a single "parameterized" test. This reduces the chances of adding a bug and makes them more readable. Parameterized tests exist in most test frameworks (JUnit, TestNG, etc...).

The right balance needs of course to be found. There is no point in factorizing test methods when the parameterized version is a lot more complex than initial tests.

This rule raises an issue when at least 3 tests could be refactored as one parameterized test with less than 4 parameters. Only test methods which have at least one duplicated statement are considered.

Noncompliant Code Example
with JUnit 5

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class AppTest
{
@Test
void test_not_null1() { // Noncompliant. The 3 following tests differ only by one hardcoded number.
setupTax();
assertNotNull(getTax(1));
}

@Test
void test_not_null2() {
  setupTax();
  assertNotNull(getTax(2));
}

@Test
void test_not_nul3l() {
  setupTax();
  assertNotNull(getTax(3));
}

@Test
void testLevel1() {  // Noncompliant. The 3 following tests differ only by a few hardcoded numbers.
    setLevel(1);
    runGame();
    assertEquals(playerHealth(), 100);
}

@Test
void testLevel2() {  // Similar test
    setLevel(2);
    runGame();
    assertEquals(playerHealth(), 200);
}

@Test
void testLevel3() {  // Similar test
    setLevel(3);
    runGame();
    assertEquals(playerHealth(), 300);
}

}
Compliant Solution
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class AppTest
{

@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void test_not_null(int arg) {
setupTax();
assertNotNull(getTax(arg));
}

@ParameterizedTest
@CsvSource({
    "1, 100",
    "2, 200",
    "3, 300",
})
void testLevels(int level, int health) {
    setLevel(level);
    runGame();
    assertEquals(playerHealth(), health);
}

}

Read more : https://rules.sonarsource.com/java/RSPEC-5976?search=tests

Lingua principale
Java
Stelle
100
Fork
105
Merge medio
2g 15h
PR unite (30g)
13

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openrewrite/rewrite-testing-frameworks

Tutte le issue di openrewrite/rewrite-testing-frameworks

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.