In this post we will focus on using a unit testing framework called JUnit which helps to organise and execute our test code efficiently.
WHAT IS JUNIT?
JUnit is a Java-based, open-source framework to help you execute unit testing. JUnit is mostly used to test unit or components of your application like classes and methods. JUnit also helps to run automated tests be it API or UI.
This post will help you use JUnit annotations in your Selenium framework. We would be looking at examples of JUnit annotations are that are used for a specific purpose in Selenium.
WHAT ARE JUNIT ANNOTATIONS?
JUnit annotations are meta-tags that provide data about the test methods and test classes outlined in our code structure. In order for our Selenium framework to execute in the most efficient manner we should add JUnit annotations in our script.
Below is the list of every JUnit annotation in Selenium that will be covered in this blog
*Note* – For our framework we shall be using JUnit 4.
In order to get started with JUnit 4 we need to add the dependency to our pom.xml as in the example below.
The @BeforeClass annotation is used to initialise objects that that could be used in our tests. When we use an object in the BeforeClass method, it would be invoked only once. The main usage of @BeforeClass annotation is used to execute code before all the tests listed in the test cases are executed.
In our example we are going to remove our public static void main method for the script. We will wrap the code for setting the property of the Chrome Driver inside of our @BeforeClass annotation. I will also add that we must make the method for using the @BeforeClass annotation static in order to use it. I will also print to the terminal window a simple statement to show the order of the test execution. This is done via the system.out.println() method.
The @Before annotation is used when we want to initialise an object or perform an action for the duration of the test method . In our framework we are going to duplicate our tests 3 times to demonstrate the usage of @Before. As a result of having 3 test methods the @Before method will be called a total of 3 times, once for each test method.
What we shall do is to move the line of code relating to creating the driver object and maximising the browser window in the @Before annotation as for each test run we would like to ensure the browser is at its maximum screen size.
The @Test annotation informs JUnit that the method that is tagged against can be run as a test A single test class may comprise numerous test methods in it tagged with @Test.
In our example we are going to duplicate the test using the nomenclature _capital letter to ensure the tests run in order for the purpose of our demo.
*Note* JUnit has the ability to add test script execution ordering. We shall explore this in later posts.
The @After annotation is used to tear down pervious actions that we would have used in previous test setups i.e. the @Before method .Tear down methods can be used to erase the state of a pervious test environment.
In our example we are going to close down the browser after each test run using the close() method.
Our @AfterClass annotation method, is executed once. It will be executed after all tests have completed. The @AfterClass tag is used to clean up operations that we would have started in our @BeforeClass method.
In our example we shall use the quit() method of Selenium to shut down all Chrome Driver system processes.
*Note* The @AfterClass annotation should use a public static method in order to be used.
The @Ignore annotation tells JUnit that the associated method to not be executed. In cases where we do not want to execute our methods we can temporarily place that block of code with the @Ignore annotation to avoid test case execution.
In our example we shall duplicate a 4th test case and tag it with @Ignore.
Now we shall execute the tests . On the BasicTest class file right click and select Run BasicTest.
Note in the terminal window we shall see all of our methods run in the following order.
From the test run we can clearly see the following JUnit execution runs:
@BeforeClass ran once.
@AfterClass ran once.
@Test ran 3 times
@Ignore has ignored the 4th test.
We can also observe how clean and concise our test class now looks with the use of JUnit.