Search code examples
data-driven-testsparameterization

What is meant by parameterization?


While reading one of the articles for Data Driven Testing, I came across a term 'parametrization of a test'. Could someone explain to me what is meant by parameterization here?


Solution

  • Let's see an example with TestNG. Suppose you have function SomeClass.calculate(int value). You want to check the results the function returns on different input values.

    With not-parametrized tests you do something like this:

    @Test
    public void testCalculate1()
    {
        assertEquals(SomeClass.calculate(VALUE1), RESULT1)
    }
    
    @Test
    public void testCalculate2()
    {
        assertEquals(SomeClass.calculate(VALUE2), RESULT2)
    }
    

    With parametrized test:

    //This test method declares that its data should be supplied by the Data Provider
    //named "calculateDataProvider"
    @Test(dataProvider = "calculateDataProvider")
    public void testCalculate(int value, int result)
    {
        assertEquals(SomeClass.calculate(value), result)
    }
    
    //This method will provide data to any test method that declares that its Data Provider
    //is named "calculateDataProvider"
    @DataProvider(name = "calculateDataProvider")
    public Object[][] createData()
    {
        return new Object[][] {
           { VALUE1, RESULT1 },
           { VALUE2, RESULT2 },
        };
    }
    

    This way, TestNG engine will generate two tests from testCalculate method, providing parameters from array, returned by createData function.

    For more details see documentation.