Sobes.tech
Junior — Middle

Can you explain what parameters in automated tests are and how they are used?

sobes.tech AI

Answer from AI

Parameters in automated tests are input data passed to the tested methods or functions to verify their behavior with different values. Using parameters allows running the same test with different data sets, which increases coverage and testing efficiency.

For example, in JUnit, you can use parameterized tests:

@RunWith(Parameterized.class)
public class CalculatorTest {
    private int input1;
    private int input2;
    private int expectedSum;

    public CalculatorTest(int input1, int input2, int expectedSum) {
        this.input1 = input1;
        this.input2 = input2;
        this.expectedSum = expectedSum;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
            {1, 2, 3},
            {5, 3, 8},
            {10, 0, 10}
        });
    }

    @Test
    public void testAdd() {
        assertEquals(expectedSum, Calculator.add(input1, input2));
    }
}

This way, the test runs multiple times with different parameters.