Srinivas Aditya
Srinivas Aditya

Reputation: 91

Difference between TestNG and JUnit execution

I was going through the differences between JUnit and TestNG and the advantages that TestNG has over Junit. I read that JUnit creates an instance of the class for each and every test method that it runs in that class. The advantage here is an error in one test method does not propagate to other test method and fail the other test method. TestNG on the other hand supports testing integrated classes which means by default, no need to create a new test class instance for every test method.

Can somebody kindly help me on how this works? I am not able to understand the propagation of failure. Does anyone has an example that clearly explains this difference?

Regards Srinivas

Upvotes: 0

Views: 735

Answers (1)

luboskrnac
luboskrnac

Reputation: 24561

I used JUnit and TestNg for long time. I am not sure what yo uare referring to in question. So I mention feature which makes TestNg my favorite. Otherwise they are comparable.

Parametrized tests:

JUnit is not very handy for parametrized tests, because when you are using using @Parametrized, test parameters are passed from static method into constructor of test class. So obviously you can have only one set of parameters per class. SO you are forced to create separate testing class per test case when using parameters. Here is example of such test.

On the other hand, TestNg has concept of @DataProvider, which enables you to have various parameter groups in one test class. So you can combine various tests with various parameters in one test class. Example here.

Upvotes: 1

Related Questions