0

I have following scenarios to Test. I would like to know which Testing framework will best fit to my requirement.

Scenario 1) Param1, Param2, Param3, Param4, Param5

I will pass above parameters with parameter Number 1, 2, 3.. till 20.

With each Number (1 to 20) a file will be generated which is Test output data. This output data I need to compare with Expected data (which is also a file), and according a result will be generated True if both the files (test output file and expected data file) are same and false otherwise.

Input to the test is as below: Param1, Param2, Param3, Param4, Param5, Number, Expected Data File (to which the test output will be compared)

Scenario 2) param1, param2, param3, param4, param5

Here different values will be assigned to above variables and again those will be passed to the test 20 times and each time different Test outputfile (total 20 outputfile) will be generated and then will be compared with the expected data file. (there are 20 files for expected data too.)

I have such 15 scenarios. which test framework will best suit here ? will Parameterized Junit be appropriate ? Please provide some guidelines too in order to use a recommended framework.

Ragini
  • 1,509
  • 7
  • 28
  • 42
  • check Spock(based on groovy) Data-driven testing - Much better than JUnit http://spockframework.org/spock/docs/1.1/data_driven_testing.html – Maddy Jan 19 '18 at 10:59
  • 2
    I think JUnit's `@Parameterized` runner is able to fulfil your needs, too. – Stefan Birkner Jan 19 '18 at 11:04

3 Answers3

0

As an example of Spock test where all params and number can be different:

@Unroll
def "scenario with different parameters"() {
    given:
    def service = new MyService()

    when:
    def actualDataFile = service.doSomething(param1, param2, param3, param4, param5, number)

    then:
    readFileAsString(actualDataFile) == readFileAsString(expectedDataFileName)

    where:
    param1 | param2 | param3 | param4 | param5 | number | expectedDataFileName
    'any'  | 'aaa'  | 'any'  | 'any'  | 'any'  | 1      | 'expectedA.txt'
    'any'  | 'aay'  | '0ny'  | 'any'  | 'any'  | 2      | 'expectedB.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 3      | 'expectedC.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 4      | 'expectedD.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 5      | 'expectedE.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 6      | 'expectedF.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 7      | 'expectedG.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 8      | 'expectedH.txt'
    // etc
}
Dmytro Maslenko
  • 2,247
  • 9
  • 16
0

1.Which test framework will best suit here ?
Ans: Class Parameterized

2.Will Parameterized Junit be appropriate ?
Ans: Yes, you are right

3.Please provide some guidelines too in order to use a recommended framework.
Ans: for asserting actual and expected result.
Assuming you want to check content of file char by char , you can use FileUtils.contentEquals(file1, file2) commons apache io

Lets see this example:

public class Calc{

    public static int add(int a, int b) {
        return a + b;
    }

}

Junit

@RunWith(value = Parameterized.class)
public class ParameterizedTest {

    private int numberA;
    private int numberB;
    private int expected;

    // Inject via constructor
    // for {8, 2, 10}, numberA = 8, numberB = 2, expected = 10
    public ParameterizedTest(int numberA, int numberB, int expected) {
        this.numberA = numberA;
        this.numberB = numberB;
        this.expected = expected;
    }

    // name attribute is optional, provide an unique name for test
    // multiple parameters, uses Collection<Object[]>
    @Parameters(name = "{index}: testAdd({0}+{1}) = {2}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {1, 1, 2},
                {2, 2, 4},
                {8, 2, 10},
                {4, 5, 9},
                {5, 5, 10}
        });
    }

    @Test
    public void test_addTwoNumbes() {
        assertThat(Calc.add(numberA, numberB), is(expected));
    }

}

Reference

VedantK
  • 9,728
  • 7
  • 66
  • 71
0

By combining Junit Parameterized Tests with some YAML Parsing, I am able to create readable Parameter Tables. Each Table Row will be parsed to a Map and each value will be parsed using a yaml parser. This way, each map contains typed instances. Even Lists as in this case.

@RunWith(Parameterized.class)
public class AnotherParameterizedTest {

    private final HashMap row;

    @Parameterized.Parameters(name="Reverse Lists Tests # {index}:")
    public static List<Map<String, Object>> data() {
        final TestData testData = new TestData(""+
             "|   ID   |       List         |  Expected   |                \n"+
             "|   0    |    [1, 2, 3]       |  [3, 2, 1]  |                \n"+
             "|   1    |    [2, 3, 5]       |  [5, 3, 2]  |                \n"+
             "|   2    |    [5, 6, 7]       |  [ 7, 6, 5] |                \n"
        );
        // parsing each row using simple YAML parser and create map per row
        return testData.getDataTable();
    }

    public AnotherParameterizedTest(HashMap obj) {
        this.row = obj;
    }

    @Test
    public void test() throws Exception {
        List orgListReversed = new ArrayList((List) row.get("List"));
        Collections.reverse(orgListReversed);
        assertEquals((List) row.get("Expected"), orgListReversed);
    }

}

Junit Test Results

Dieter
  • 89
  • 1
  • 4