Tuesday 27 May 2014

Introduction to Junit

Introduction to Junit

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

Junit has become the standard tool for Test-Driven Development in Java (see Junit.org)


Architecture of Junit 

JUnit test framework is a package of classes that lets you write tests for each method, then easily run those tests













How Junit Works ?

 
 

 Junit annotations


To execute Selenium WebDriver testing using JUnit we have to add JUnit annotations in your test.

  1. @Before
  2. @After
  3. @BeforeClass
  4. @AfterClass
Test methods must be annotated by the @Test annotation. If the situation requires it, it is also possible to define a method to execute before or after of the test methods with the @Before or @After and @BeforeClass or @AfterClass annotations.
Let’s elaborate on these JUnit annotations here:

@Test

The annotation @Test identifies that a method is a test method. When it is mentioned as @Test before a test method, it tells JUnit framework that the following is a Test Method.

@Before

The @Before annotation is used to identify the method which is executed before executing the Test Method. This method can be used to set up the test environment.

@After

The @After annotation is method which is executed after executing the Test Method. This method can be used to do teardown i.e. deleting temporary data or setting up default values or cleaning up test environment etc.

@BeforeClass

The @BeforeClass method is used only once before start all tests. Basically this is used to perform time intensive activities, like connect to a database.

@AfterClass

The @AfterClass method is used only once after finish executing all tests. Basically this is used to perform clean-up activities, like disconnect from a database.
In upcoming article the we will use above listed JUnit annotations and create the Selenium WebDriver automation tests.

1 comment: