
TestNG is a tool similar to JUnit for testing applications that provides extra functionalities. Let’s see some recommendations to use it better:
- Define sets of tests (testSuites) in xml files
- Separate unit tests (test a specific functionality) of integration ones
- Use the description option and document in javadoc
- Write the data in xml or properties files
- Do not catch exceptions in tests
- Continuous Integration
- Use the methods @before and @after
Define sets of tests (testSuites) in xml files
Doing so you can test only a part of the application, check minimal functionality (happy path), etc.
Separate unit tests of integration ones
This makes it easier to diagnose where the errors occur when we run them on a pipeline and it fails.
Use the description option and document in javadoc
Document in the javadoc of each method the test steps and the expected results. Doing so it will be easier to fix it when it fails by a change in the application.
Write the data in xml or properties files
Instead of hardcoding them in Java code. Normally the data are repeated on multiple tests and if they change, you will only have to change them in one place.
Do not catch exceptions in tests
An exception should be thrown and thus abort the test if something fails.
Continuous Integration
It is easy to configure your CI system to launch all tests every time a version is deployed or when the code is committed, so the errors will be detected as soon as possible. This will also give you the peace of mind that in the new version still works everything of the previous one.
Use the methods @before and @after
To group the steps that should be executed at the beginning and end of all the methods of a class. This avoids code repetition and the changes will only be modified in one place. Do you know other best practices? You can share them in the comments

Leave a Reply