Monday, July 18, 2016

Web Test Automation

Most of tech companies now realise that manual testing is not as effective as anticipated and wanted to automate the whole process of users going to their websites performing some actions, submitting a form, or triggering a search,... etc.

You have probably thought many times of automating some repetitive tasks that you do every day or sometimes multiple times per day. Some people automate making their morning coffee - Believe me :).

Testing is one of the repetitive tasks that developers tend to do very often everyday .. Why? because it's important to make sure that things are working as expected before pushing them to production.

In this post I will not talk about the theory of automation or the frameworks themselves as there are plenty of articles for this purpose .. This article is just to introduce you to the world of automation and point you towards the next step.

Links to read

 

Phantom JS
Selenium headless mode

Steps to run this example

  • Create a maven project and copy these files
  • Make sure you download chrome driver from here
  • Edit the path to your chrome driver installation
  • Run the Example.java class

Example


In this example I will use Selenium framework to test Amazon DE website and search for something there .. then print the search page title .. No assertions/checks will be done as it depends on your business and the purpose of your test.

Pom.xml that defines libraries used:



Cheers ;)

Thursday, March 10, 2016

Mockito for better unit tests

Today I'm going to introduce you a very nice framework that allows you write good unit tests that cover only the parts of code that you want to test and stub all other dependencies .. but obviously in order to do that your code must be written in a testable way.

To know more about testability please refer to this wiki document.

Agenda
  • What is Mockito?
  • How to use it?
  • Example
What is Mockito?

Mockito is a framework that  allows you writing unit tests that only test your piece of code by mocking and stub all other components that your piece of code depends on.

In other words if you want to test your integration with other libs and components then its not unit test any more .. its called integration test which is not covered in this post.

How to use it?

You can configure mockito using maven or simply download the lib from here.

For maven configuration you can define the dependency in your pom.xml as here.

Example

Note: I will use junit in the following example and in case you are not familiar with junit please refer to this.

Let's start by a very simple example that you have a class that reads a comma separated String and return it as a List<String> .. It reads this String by calling another class CSVReader that can either read it from file/database/network .. so in case you want to test your class only without being dependant on the CSVLineReaderClass you should mock the reader class.

Why?

Usually when running unit tests you need to get rid of the overhead of setting up other dependencies and want to see how your class/function behaves not how others behave.

Implementation of CSVLineParser:


The unit test should look like this, and you can add more test cases :).



When you run the test cases you will see the nice green results which means that all cases have passed correctly.



When you do something wrong like this :) .. you will see the unit tests failing.

 



In fact that's really nice, its really safe when you or someone else in your team mistakenly does these kind of stupid issues, its better to catch it during development than catching it in pre-production or on production and embrasse yourself and your team :D.

Mockito has way more than this simple example, but as usual I like to introduce you with very small nice things and then you can continue reading and see other features of the framework :).

I hope you like it.