In this post, we’ll go over the simple task of creating test categories that can then be executed using a Gradle task command. This can be extremely helpful for a tester when creating test suites. Each test can be given a ‘category tag’ which will then become part of that test suit.
In order to do this, you’ll need the following:
1. A Gradle project (with a build.gradle file)
2. About 5 minutes
Creating the Test Category
Within the src/test/java directory create 2 packages. category and test. The category folder must be named as such for the runner to pick up the categories while the test folder can be called anything (it’s just where we will be keeping the actual test code)
Within the ‘category’ folder create an interface called ‘Integration’. You may call this whatever the name of your category.
Adding the category to the test
Now let’s create a sample test and add the ‘Integration’ Category to the test:
So all we’ve done here (apart from the standard JUnit stuff) is add a category annotation, with the name of the interface as the argument. if you added more than a single category you would be able to insert any one or even more than one as your arguments. To add more categories, just separate by using commas. (it should automatically enclose all the arguments within curly braces.
Creating the Gradle task to run the test
So now we have our test categorised. The final step is to add a task to our Gradle file to run the tests.
Within the build.gradle file, add the following:
This is a simple Gradle test task with the category being specified. The name that has been given to this task is ‘integrationtests’.
Running the test
If you’re using IntelliJ you should now see a task called ‘integrationtests’ within the Gradle control panel. (view/Tool Windows/Gradle to open the Gradle control panel).
You can run the test suite by clicking on the task or by executing the following Gradle command
gradle integrationtests
Viewing the Test Report
within the application folder, find the reports within build/reports/tests and you will see a folder called ‘integrationtests’ or the name you gave your Gradle task.
clicking on the index.html will open the test report.