Tuesday 8 July 2014

What is Junit Framework in Selenium



What is TestNG and JUnit frameworks?

-> TestNG and JUnit are tools  to help you organize our test.

-> We can define what you want to run before/after tests and we can able to group the tests and reporting them to made easier.


How are TestNG and JUnit frameworks are related to selenium?

-> Those frameworks are usually mentioned with Selenium.

-> Selenium is common testing tool. But selenium combined with any framework (TestNG or JUnit) together do some work. But both are completly different

-> Selenium used connects you to your browser

-> TestNG/JUnit used to organizes the tests




What is JUnit Framework in Selenium Webdriver

-> JUnit is used the most, then TestNG, then FitNesse

-> JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

-> We have using some JUnit annotations to control the flow and activity of code execution.

-> You must need to insert JUnit annotation inside the java code to execute your test case as junit test case.

-> The important annotations are

1) @Before
2) @Test
3) @After
4) @BeforeClass
5) @AfterClass.



1. @Before Annotation 

-> THe method under @Before annotation will be executed before the moethod written under @Test annotation.
-> Generally method under @Before used to initializing browser or environment related setup.
-> The method under @Before annotation will executed before each @Test annotations. That means if we have four @Test annotaions in your class, then the @Before method will execute four times.



2. @After Annotation

-> The method under @After annotation will execute after completion of @Test method execution.
-> Generally @After annoation used to close the browser and release the memory of object which created in JUnit framework.
-> The method under @After annotation will executed after each @Test annotations. That means if we have four @Test annotaions in your class, then the @After method will execute four times.


3. @Test

-> The method under @Test annotation will execute after completion of @Before method execution.
-> Generallu method under @Test used to write all testing related activity.
-> We can have any number of @Test method in single class.


4. @BeforeClass

-> Methods under @BeforeClass will execute before any one of the test method starts execution.
-> It will execute only once even if there are multiple @Test method in our class.


5. @AfterClass

-> The method under @After class will excute all the completion of @Test methods exection in class.
-> It will execute only once even if there are multiple @Test method in our class.



Difference between @Before and @BeforeClass annotations


-> The method under @Before annotation will executed before each @Test annotations. That means if we have four @Test annotaions in your class, then the @Before method will execute four times.
->

Test method marked with @Before annotation will be executed before the each @Test method. Means if there are 5 @Test methods in your class then @Before test method will be executed 5 times.
Test method marked with @BeforeClass annotation will be executed just before the class. Means @BeforeClass method will be executed only once before the class even if there are 5 @Test methods in your class.


Difference between @After and @AfterClass annotations

Same as @Before annotation, Test method marked with @After annotation will be executed after the each @Test method.
@AfterClass annotation will be executed only once after last @Test method executed.



No comments:

Post a Comment