Sunday, April 4, 2010

What is TestNG? And whats the difference between Junit and TestNG

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:

  • Annotations.

  • Flexible test configuration.

  • Support for data-driven testing (with @DataProvider).

  • Support for parameters.

  • Allows distribution of tests on slave machines.

  • Powerful execution model (no more TestSuite).

  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).

  • Dependent methods for application server testing.

If you want to learn TestNG you can download it and see its documentation at “http://testng.org/doc/index.html”.

TestNG is extensively used in Selenium automation and Code testing instead of Junit due to its advantages over it.


Using TestNG Selenium automation becomes simplified and also the Parallel execution using Selenium Grid becomes easy.


TestNG have a lot of features in it and hence I wont be covering every thing in my blog. But will only talk about advantages of TestNG over Junit. I will be discussing about different ways of using TestNg functions in my coming blogs.


Advantages of TestNG over Junit

  1. In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.

  2. Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite,@Before/AfterTest and @Before/AfterGroup

  3. No Need to extend any class in TestNG.

  4. There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG

  5. In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.

  6. Grouping of testcases is available in TestNG where as the same is not available in Junit.

  7. Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity” and TestNG will automatically execute the cases belonging to the “Sanity” group.

  8. Also using TestNG your selenium testcase execution can be done in parallel.


20 comments :

Gergely said...

Hi!

Great post. I just have one question.

Regarding this:
"In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test."

Could you please tell me how is that good? I learned that tests should never ever depend on other tests. That is not a good practice to do since when there is something wrong with one test ( ex: Typo ) all the others will also fail because of that typo.

Varun Menon said...

Hi Gergely,

You are correct that tests should be independent.

But in testing many times you are required to depend on some other testcases.
For ex. There is a create document testcase and there is a Edit document case. Now you cannot Edit a document until it has been created.
The solution to execute edit document case can be that you manually go and create a document or you call the create document method inside your Edit document method but that becomes tedious again.Hence it is easy to say that edit depends upon create.

By the way while coding you can tell TestNG that if any test method on which other test methods depends fails then whether the dependent methods needs to be executed or skipped. By default such cases are skipped by TestNG. So even one test fails other tests depending on the failed test case are skipped instead of executing or failing them.

MANOJ said...
This comment has been removed by the author.
MANOJ said...

Hi
Running the test cases parallel'y is possible using TestNg ?

How it will be different from Selenium GRID ?

Varun Menon said...

@Manoj: Yes running cases in parallel is very much possible in TestNg. And thats one of the common reason that people use TestNg so much.

TestNg is a test execution framework for java, that can execute testcases in parallel.
Where as Grid is an parallel execution server for selenium. It keeps track of all the RC registered with it and based on the test case request, executes the testcase code on a particular RC.

Both are independent of each other.

Deepthi said...

Useful info with example.

Can you tell me once we search for multiple phrase in google or any site take a screen shot of every result and close the browser so that instead of checking each browser we can check the screen shots for o/p's.

Varun Menon said...

This depends upon your implementation.
You can write a screenshot taking function and call it after every test case execution using the testNG listeners.

Deepthi said...

yes varun ... i'm able to take screen shot ... can you help to write a code for closing browser.

Varun Menon said...

Are you not doing driver.quit after every test execution? If not try to initialize driver object in the BeforeMethod and driver.quit in AfterMethod

ankita said...

Hi,
I am trying to validate the character count of a text field using TestNG, & I am not getting how to do so.

Here's my code what I have done, where upper limit is 50:-

String Add="";
int i;
for(i=1;i<=51;i++)
{
Add+="A";
}
selenium.type("id=TextBox123", Add);
string text=getText("id=TextBox123");
int n=text.length();
if(n==50){
//continue further
}
else
{
test.fail(null);
}

I know I am doing something wrong but unable to understand what.

Subrahmanyam said...

Varun, the 1st difference is not correct. You can have just @Test in a single Class. There is no need for After/BeforeClass.

Varun Menon said...

I accept. This post is pretty old and was written in my starting days.
My mistake of not reviewing it :).
Anyways things had changed over a period of time and now Juni supports many features similar to TestNg but I feel they are complex to understand and implement.

pallavi said...

junit life cycle:Itcontains multiple anotations like @before @test @after
when we run the script first it will invoke the method which is available under @before anotation then it executes the @test method and then it executes @after method
this process will be continued for all methods
once the Execution is done it provides the results under junit section
testnglife cycle:
Itcontains multiple anotations like @before Test, @test , @after Test
when we run the script first it will invoke the method which is available under @before anotation then it executes the @test method(alphabetical order of all methods) and then it executes @after method
it creates a folder called Testoutput undr the project of bydefault
if u want to ignore the test from execution add the command (enabled=false)at @test method

uma said...

What is genkins? Please clear with me.

Varun Menon said...

I think you are referring to Jenkins or Hudson. It a continuous integration tool which help helps in automating build, deployment and testing process.

rajkumar said...

How we can execute tests parallelly using TestNG?
can you please provide one example...

Anonymous said...

is there any difference between testing and testNG

Varun Menon said...

Yes. Testing is nothing but verifying and validating the quality of product. Where as TestNG is a unit test framework to achieve a part of testing using code.

Mukesh Otwani said...
This comment has been removed by a blog administrator.
Varun Menon said...
This comment has been removed by the author.

Post a Comment