Comparative Analysis of Junit and Testng Framework
Total Page:16
File Type:pdf, Size:1020Kb
International Research Journal of Engineering and Technology (IRJET) e-ISSN: 2395-0056 Volume: 05 Issue: 05 | May-2018 www.irjet.net p-ISSN: 2395-0072 Comparative Analysis of JUnit and TestNG framework Manasi Patil1, Mona Deshmukh2 Student, Dept. of MCA, VES Institute of Technology, Maharashtra, India1 Professor, Dept. of MCA, VES Institute of Technology, Maharashtra, India2 ---------------------------------------------------------------------***--------------------------------------------------------------------- Abstract - Testing is an important phase in SDLC, Testing It is flexible than Junit and supports parametrization, parallel can be manual or automated. Nowadays Automation testing is execution and data driven testing. widely used to find defects and to ensure the correctness and The Table – 1 compares different functionalities of TestNG completeness of the software. Open source framework can be and JUnit framework. used for automation testing such as Robotframework, Junit, Spock, NUnit, TestNG, Jasmin, Mocha etc. This paper compares Table -1: Functionality TestNG vs JUnit JUnit and TestNG based on their features and functionalities. Key Words: JUnit, TestNG, Automation Framework, Functionality TestNG JUnit Automation Testing, Selenium TestNG, Selenium JUnit Yes 1. INTRODUCTION Support Annotations Yes Yes Support Test Suit Initialization Yes Testing a software manually is tedious work, and takes lot of No time and efforts. Automation saves lot of time and money, Support for Tests groups Yes also it increases the test coverage and improves accuracy. It Limited Support Parametrized Test Yes helps developers as well as testers. Choosing the right support automation framework is crucial, so that it helps different Yes kind of testing such as unit, functional, regression, volume or Support Exception Test Yes data driven testing. While choosing the right framework, it is No important to take care of some factors such as framework Support Dependency Test Yes should be reusable, and code should be maintainable, proper Yes organisation and readability of the code, time optimization in Support Ignore or Disable Test Yes No terms of writing Tests and framework should have reporting Support Parallel Testing Yes mechanism. All frameworks are different and covers Yes different key factors which are compared below. Support Maven Yes Support Data driven approach or Yes with Data No dynamic input provider 1.1 Framework No Support for Tests Reports Yes Framework is a collection of rule or guidelines to interact Yes IDE support Yes with components which are interlinked. These components are responsible for the creation and execution of automated Tests and reporting for results after Test execution. 4. Annotations 2. JUnit In simple language annotations are metadata for the code, i.e. data about the data. Annotations stats with ‘@’ followed It is popular open source java based light weight by annotation name. They are introduced java JDK 5.0 framework designed by Kent Beck, Erich Gamma and it is onwards. They are applied before the method. Complier hosted at Github. It is test driven development (TDD) used checks for annotations and consider the following method as for writing and running Tests. It uses annotations for overridden method. Complier checks for existence of the identifying Tests, which are written as methods. It is a part of same method in the parent class otherwise throws a XUnit, Unit testing family. The current version used is Junit 5. compiler error. Annotations does not directly affect the program execution but provide compile time instructions to 3. TestNG the complier. The Table – 2 gives the list of different annotations of TestNG It is an open source framework where NG stands for Next and JUnit framework for similar functionalities. Generation in TestNG. This framework is inspired by JUnit and NUnit, but it overcomes the JUnit flaws. TestNG is user friendly and it covers unit, integration and functional testing. © 2018, IRJET | Impact Factor value: 6.171 | ISO 9001:2008 Certified Journal | Page 4144 International Research Journal of Engineering and Technology (IRJET) e-ISSN: 2395-0056 Volume: 05 Issue: 05 | May-2018 www.irjet.net p-ISSN: 2395-0072 Table - 2: Annotations Support TestNG vs JUnit } @Test(timeout = 1000) Public void Timeout_Method() { System.out.println("Annotation @Test(timeout) - enforce timeout in Annotations TestNG JUnit test case"); Annotations Annotations } @Test(expected = ArithmeticException.class) public void Expected_Method() { @Test System.out.println("Annotation @Test(expected) - test for specified @Test For test annotations exception during its execution"); Applied to methods to run @BeforeSuite - int i = 10/0; before all tests in a Test Suite } } Applied to methods to run after @AfterSuite - all tests in a Test Suite Applied to methods to run @BeforeTest Following are TestNG code snippets for above - before the test annotations: Applied to methods to run after @AfterTest - the test public class TestNGAnnotations { Applied to methods to run @BeforeGroups - before the first Tests from the @BeforeGroups("Regression") same group public void BeforeGroups_Method() { Applied to methods to run after @AfterGroups System.out.println("Annotation @BeforeGroups - before test executed the first Tests from the same from same group"); group } Applied to methods to run @BeforeClass @AfterGroups("Regression") @BeforeClass before the first method in a public void AfterGroups_Method() { class is invoked System.out.println("Annotation @AfterGroups - after test executed from Applied to methods to run after @AfterClass same group"); @AfterClass the first method in a class is } invoked @BeforeClass Applied to the methods to run @BeforeMethod public void BeforeClass_Method() { @Before before each test method System.out.println("Annotation @BeforeClass - before all test cases "); } Applied to the methods to run @AfterMethod @After @AfterClass after each test method public static void AfterClass_Method() { To ignore a test method @ignore @Test(enabled System.out.println("Annotation @AfterClass - after all test cases"); = false) } @BeforeMethod Timeout for test method @Test(timeout = @Test(timeout public void Before_Method() { 1000) = 1000) System.out.println("Annotation @Before - before each test cases "); } @AfterMethod 5. Annotations Syntax public void After_Method() { System.out.println("Annotation @After - after each test cases"); } Following are JUnit code snippets for above annotations: @Test(groups = "Regression") public void Test_Method1(){ public class JUnitAnnotations { System.out.println("@Test - Included in the Regression group"); } @BeforeClass @Test public static void BeforeClass_Method() { public void Test_Method2() { System.out.println("Annotation @BeforeClass - before all test cases "); System.out.println("@Test - Not included in any group"); } } @Before @Test(timeout = 1000) public void Before_Method() { public void Timeout_Method() { System.out.println("Annotation @Before - before each test cases "); System.out.println("Annotation @Test(timeout) - enforce timeout in } test case"); @AfterClass } public static void AfterClass_Method() { @Test(enabled=false) System.out.println("Annotation @AfterClass - after all test cases"); public void Ignore_Method() { } System.out.println("This method is ignored"); @After } public void After_Method() { System.out.println("Annotation @After - after each test cases"); @Test(expected = ArithmeticException.class) } public void Expected_Method() { @Test System.out.println("Annotation @Test(expected) - test for specified public void Test_Method(int a, int b){ exception during its execution"); int add = a+b; int i = 10/0; assertEquals (add,10,"Assertion is correct"); } } } @Ignore public void Ignore_Method() { System.out.println("Annotation @Ignore - this method is ignored"); © 2018, IRJET | Impact Factor value: 6.171 | ISO 9001:2008 Certified Journal | Page 4145 International Research Journal of Engineering and Technology (IRJET) e-ISSN: 2395-0056 Volume: 05 Issue: 05 | May-2018 www.irjet.net p-ISSN: 2395-0072 3. CONCLUSIONS As per the above study both the frameworks are good for automation testing. Though JUnit is most popular and widely used testing framework, it does not offer much functionalities as compared to TestNG such as grouping, parallelism, dataProvider/ parametrization, reporting. TestNG provides better flexibility than JUnit. REFERENCES [1] http://toolsqa.com/selenium-webdriver/testng-tutorial [2] https://en.wikipedia.org/wiki/JUnit [3] http://toolsqa.com/java/junit-framework/junit- introduction [4] http://www.java2novice.com/junit-examples/junit- annotations [5] https://www.tutorialspoint.com/testng/testng_basic_an notations.htm © 2018, IRJET | Impact Factor value: 6.171 | ISO 9001:2008 Certified Journal | Page 4146 .