0

I'm working on test automation for shop basket module and I'm struggling with providing test data using Junit CsvSource in the 'nice way'.

So far my test looks like this

@ParameterizedTest(name = "Total quantity and price in the cart is calculated correctly for various type items")
    @CsvSource({
            "Ball, 3, Glasses, 5, Cable, 15, Monitor, 25, Cube, 11",
            "Camera, 10, Ball, 33, Pillow, 1, Notebook, 19, Headphones, 2"
    })
    void quantityAndPriceIsCorrectForDifferentTypesOfProducts(String name1, String amount1,
                                                              String name2, String amount2,
                                                              String name3, String amount3,
                                                              String name4, String amount4,
                                                              String name5, String amount5) {
        task1Page = new Task1Page(driver);
        task1Page.open();

        List<String> names = List.of(name1, name2, name3, name4, name5);
        List<String> amounts = List.of(amount1, amount2, amount3, amount4, amount5);

        task1Page.addItemsToBasketFromTheList(names, amounts);
        task1Page.verifyQuantityOfItemsInBasketFromList(names, amounts);
        task1Page.verifyTotalPriceInBasketFromList(names, amounts);
...
}

With my current implementation I'm limited to 5 item types only. And I would like to also include cases with more products. Is there any nice way to handle that case?

1

It looks like you're using Junit 5. If that's the case you can take advantage of the MethodSource annotation, which allows you to create a method that returns the values you want the tests to run with.

In the example below Product is just a class that's storing my test data.

    @ParameterizedTest
    @MethodSource("populateValues")
    public void tempTest(ArrayList<Product> products) {
        for(Product product : products) {
            System.out.println(product.getName() + ": " + product.getAmount());
        }
    }

    public static ArrayList<ArrayList<Product>> populateValues() {
        ArrayList<ArrayList<Product>> allProducts = new ArrayList<>();

        ArrayList<Product> run1 = new ArrayList<>();
        run1.add(new Product("Cat food", 5));
        run1.add(new Product("Dog food", 12));

        ArrayList<Product> run2 = new ArrayList<>();
        run2.add(new Product("Gerbil foood", 5));
        run2.add(new Product("Hamster food", 8));
        run2.add(new Product("Parrot food", 1));

        allProducts.add(run1);
        allProducts.add(run2);

        return allProducts;
    }

The output of the tests:

1)
Cat food: 5
Dog food: 12

2)
Gerbil foood: 5
Hamster food: 12
Parrot food: 12

As you can see, this allows you to have an arbitrary number of products per test.

0

You can testNg Data Provider to run parameterized tests. But if you want to stick with JUnit, you could use Junit DataProvider. Morever, you might need to find a way to use the dynamic data instead of specifying in the java code (like CSV,JSON,YAML etc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.