Hello Folks,
Let’s learn something new today. We have used @Test annotation on methods. Can we use @Test annotation on a class?
Yes, We can use @Test annotation on class.
Suppose, you need to write 10 @Test annotated method in a class. Either you mark all methods as @Test annotated or directly annotate class itself as @Test. All method will be treated as @Test annotated method by default. See an example below:
TestNG class:
Run as a testng suite:
What will happen if I need to write other annotated method like BeforeMethod or AfterMethod? Not a big deal. Mark required annotation on method. It will override class level @Test annotation. Example is as below:
TestNG class:
Output:
You can see Tests run count is 2. It means @Test annotation applied only for method without any annotation.
Another scenario: Suppose you have a class which is @Test annotated at class level. You have a sub class extending super class. Sub class is not @Test annotated method. Because of inheritance, automatically sub class will also become @Test annotated. Confused?? See below example:
Run it. You will get below output:
Liked the post? If yes please share and let others also learn.
More about TestNG in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium
Hello Folks, We have seen previously that how can we parameterize methods in a TestNG class. The approach of using…
Hello Folks, I was asked this question in an interview and I was like "Are there two terms "Parameters" and…
Hello Folks, Arguments or Parameters help us to achieve reusability of codes and cleanliness of codes. We can do overloading of…
Hello Programers, Problem: How to find duplicate values in each row of 2D array. For example , we have 2d…
Hello Folks, Recently I have started learning or exploring more on best practises in Selenium Framework and same I want…
Hello Folks, In this post we will learn below topics: 1. How to skip a test conditionally? 2. How conditionally…