Mark Class with @Test Annotation

Sometimes you might have few test cases to few hundred test cases in a class, In such times it becomes difficult to annotate every single method as test method as @Test.

To avoid such pain TestNG provides a way to annotate the test class itself with @Test method. When you annotate test class with @Test, then all the methods including static and non-static methods become as test cases like actually we annoted them with @Test.

When we make class level @Test annotation, all the public methods of this class will become test methods even if they are not annotated.

We can still define @Test annotation on of the method if we want to add any attributes to particular test method.

In below example we have marked few methods as @Test and total class as @Test, just to check whether it considers all methods as @test or only annotated


@Test
public class TestJenkins {
	
	@Test
	public void cherCherTech() 
	{
		System.out.println("cherCherTech");
	}
	public void google() 
	{
		System.out.println("google");
	}
	public void yahoo() 
	{
		System.out.println("yahoo");
	}
	public static void staticmethod() 
	{
		System.out.println("staticMethod");
	}	
}			


class-level-test-testng

Disable/Ignore a test class in TestNG:

Like Test method we can also disable a class if we want and no method in the class will get executed.

We can disable a class using enabled = false


@Test(enabled=false)
public class TestJenkins 
{
	public  void sampleTest() {
		System.out.println("I am a test without @Test");
	}
}			


notest-class-level-testng

What I realized :

The settings you make at @Test in class level will affect every single test individually.

For example if you set the invocationCount as 5, then each and every method will be executed those many time, yes that is what it does with method so what is the differecnce karthiq.

The difference is it executes the first test 5 times then moves to second method and executes that method 5 times, so on.

invocationCount :

When we provide invocationCount paameter to @Test with class, invocationCount runs every test with given number of times, like it will execute first method for 5 times then moves to second method.


@Test(invocationCount=3, invocationTimeOut=7000)
public class TestJenkins 
{
	public  void sampleTest() {
		System.out.println("I am a sample test without @Test");
	}
	
	public  void test() throws InterruptedException {
		System.out.println("I am a test with @Test");
		Thread.sleep(3000);
	}
}			


invocationcount-class-level

InvocationTimeout at class level with @Test Annotation:

InvocationTimeout sets the maximum time for every method present in the class, InvocationTimeout doesn't expect a class to complete in that given time


@Test(invocationCount=3, invocationTimeOut=7000)
public class TestJenkins 
{
	public  void sampleTest() {
		System.out.println("I am a sample test without @Test");
	}

	public  void test() throws InterruptedException {
		System.out.println("I am a test with @Test");
		Thread.sleep(3000);
	}
}			


class-level-invocationtion-timeout

ThreadPoolSize at class level :

ThreadPoolSize set the number of thread for each test method, In below example I have given that invocationCount as 3 and there are two methods inside, which will make 6 invocations so I have provided 6 threads but still 3 thread will execute first method and same will come for second method as well.

The conclusion is settings applies to each test not to the whole tests.

	
@Test(invocationCount=3, threadPoolSize=6)
public class TestJenkins 
{
	public  void sampleTest() {
		System.out.println("sampleTest Thread name : "+Thread.currentThread().getName());
	}

	public  void test() throws InterruptedException {
		System.out.println("test Thread name : "+Thread.currentThread().getName());
		
	}
}						


threadpool-size-class-level

singleThreaded parameter at class level @Test Annotation :

singleThreaded is class level parameter for @Test annotations,, you cannot use this parameter along with method.

singleThreaded makes sure that all the methods of this class gets executed by same thread.


@Test(singleThreaded=true, invocationCount=3)
public class TestJenkins 
{
	public  void sampleTest() {
		System.out.println("sampleTest Thread name : "+Thread.currentThread().getName());
	}

	public  void test() throws InterruptedException {
		System.out.println("test Thread name : "+Thread.currentThread().getName());
		
	}
}			


singleThreaded-testng-class

singleThreaded vs ThreadPoolSize :

When a class @Test hosts singleThreaded and ThreadPoolSize parameter then importance will be give to the ThreadPoolSize.

So the program will not be executed in single thread, it will try to run in different thread and different number of thread specified by ThreadPoolSize.


@Test(singleThreaded=true, invocationCount=3, threadPoolSize=6)
public class TestJenkins 
{
	public  void sampleTest() {
		System.out.println("sampleTest Thread name : "+Thread.currentThread().getName());
	}

	public  void test() throws InterruptedException {
		System.out.println("test Thread name : "+Thread.currentThread().getName());	
	}
}			


threadpoolsize-singlethread-in-testng-class Remaining parameter works as they are designed and I have explained it in previous article.

Sub Class @Test Annotation

So far we have seen what if a class has @Test annotation but what would happen if some class extends a Base class.

Base class have been already marked with @Test, as usual the sub class will inherit all the properties from the Base class(except private items).

So sub class will have same setup as like Base class and the Base class configuration are applicable for sub class as well.

Everything goes well except one thing, Base class method are executed twice.


@Test
public class TestJenkins 
{
	public  void sampleTest() {
		System.out.println("Base class");
	}
}
class StupidSubClass extends TestJenkins
{
	public  void dummyTest() {
		System.out.println("Sub class");
	}
}			


inheritance-test-classes-testng

Sub Class Options with @Test :

We can provide our own sub-class @Test parameters i.e overriding the parent configurations.

But still the Parent class got executed twice, are you getting same doubt as me like may Constructor & Super() might have influence.


@Test
public class TestJenkins 
{
	public  void sampleTest() {
		System.out.println("Base class");
	}
}

@Test(enabled=false)
class StupidSubClass extends TestJenkins
{
	public  void dummyTest() {
		System.out.println("Sub class");
	}
}			


parent-invoked-twice-testng

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech