Search code examples
javatestng

Is there some reason to make TestNG @Test annotated methods public versus private?


Is there some reason to make TestNG @Test annotated methods public versus private?

I want to be consistent but not sure which one to use.

@Test
private void testMyStuff() {}

or

@Test
public void testMyStuff() {}

or other?


Solution

  • I am not an expert especially with TestNG but regularly write unit tests in Java.

    I recommend to remove the visibility keyword completely from the test methods for improved readability (making them package private or default visible).

    @Test
    void testMyStuff() {}
    

    If TestNG allows for this, it simply will reduce the number of characters you visually have to read to understand the code. The visibility does not add anything useful to the information you need for a test method. You also will save time typing it.