IBM Cloud Devops and Unit Testing (Article)
Total Page:16
File Type:pdf, Size:1020Kb
Article Unit Tests and IBM Cloud DevOps Unit tests and IBM CLoud DevOps Technical and business point of view Written by Diego Sanchez Schenone Introduction This article is meant to share my experience about two important aspects of the software development process to be kept in mind if you want to improve the quality of your outcomes. Unit Tests and Continuous Delivery are pillars of the development process which provide a solid foundation to deliver products and services of quality. They help and improve in many ways the methodology we produce software. Another important subject to be presented is “DevOps”. In the last years we heard a lot about this term. From my point of view, “DevOps” is the right approach to improve the process we develop software and it should be implemented by software companies that want or need to enhance their development process. The article is divided in three parts. Firstly, we are going to discuss what are unit tests and which benefits they bring to us. Secondly, we will show how to use JUnit to implement unit tests in Java language. Finally, we are going to have a first look about IBM Cloud DevOps and tell our real experience of using it. Part1: What is a unit test Unit testing can be defined as a kind of software testing which is focused on individual unit components of a software program. These units must be the smallest testable components. Depending on the programming paradigm these units can be methods if we talk about the object oriented programing, or functions in the functional paradigm. They must have the characteristic of being locally isolated to be tested (must not be related to another components). In this testing model, we usually group a set of these components conforming a Test Suite which bundles tests that are locally related. An important question to be asked is: When do they must be coded?. And the answer is simple: As the develop of the software components grows simultaneously unit tests must be designed and implemented for each of them. Based on our experience they must not put off for the end because it is too difficult to do it and purposeless (we need to tests what we develop as soon as possible). Technologies for Business - Article Unit Tests and IBM Cloud DevOps Which benefits do they bring to us? Unit tests bring many benefits to the software development process which make it controllable and measurable, among others we have the following. ❏ Check if the methods/functions work as expected. ❏ Identify failures in the implementation of the algorithms. ❏ Covering more user scenarios by creating test suites. ❏ It is a “must” to improve and control the quality of the product. ❏ They are an essential part to implement Continuous Integration and Delivery. Feature Benefits Software Development Business Process Unit test Check and measure every Measure the product. If single component from the something can be measured then very beginning. can be managed. Test Suites More testing scenarios are Offer a higher quality product added gradually according because more use cases are to the software is growing. covered to be tested. Continuous Delivery Automatization Cost reduction Manual and non values tasks are removed from the process. Be more competitive because developing times are optimized given this way a fast response to the business needs. Benefits of implementing unit testing Quick recap ● How big the size of the component to be tested should be? The smallest component such as function, method or procedure. ● What is a Test Suite? A group of unit tests that are tested together. ● When do they must be coded? Just after finished a software component. ● When do they must be executed? After any change has been added. ● How often they be executed? They should be a part of Continuous Delivery therefore executed after any building. Diego Sanchez Schenone 1 Technologies for Business - Article Unit Tests and IBM Cloud DevOps Part 2: JUnit JUnit is a framework designed to write and run unit tests for the Java language. It provides lots of good features to build unit tests. Through a simple but helpful example we are going to explain the main concepts and features of JUnit. The software to be tested We are in charge of developing two classes which represent arrays of numbers. This components are part of a Math system that runs online in a web application. Class Description NumberArray This class represents an Array that only can contain integer numbers and does not allow repeated elements. It has the following methods: public boolean addValue(Integer value) Add the argument in the array If it does not contain it. public void removeAllElements() Remove all elements. public boolean removeValue(Integer r) Remove the argument from the array. public boolean containsValue(Integer value) Return true if the argument belongs to the array. public boolean isEmpty() Return true is the array is empty, otherwise false. NumberRangeArray This class extends NumberArray. It overrides the method addValue to control if the argument is between a given range. This class has two attributes minValue and maxValue to set the valid range of its elements. public boolean addValue(Integer value) It controls if the argument is between the minValue and maxValue to be added into the array. If not it returns false. Immediately after we developed them we write the tests to verify if the methods respond as we expected. We are going to develop the following classes to test them. In JUnit “a test” is represented by a method using the Java annotation @Test. Diego Sanchez Schenone 2 Technologies for Business - Article Unit Tests and IBM Cloud DevOps Class Description NumberArrayTest In this class we develop the following testing methods. @Test public void testIsEmpty() If checks if the array is empty. @Test public void testAddValueOk() It verifies if a number that not belongs to the Array is added. @Test public void testAddValueNotOk() It verifies if a number that belongs to the Array is not added when is intended to be added. NumberRangeArrayTest To test the class NumberRangeArray we are going to develop the following tests. Each has the responsibility the only check one method. @Test public void testIsEmpty() If checks if the array is empty. @Test public void testAddValueOk() It verifies if a number that is in range (between minValue and maxValue) is added. @Test public void testAddValueNotOk() It verifies if a number that is not in range (between minValue and maxValue) is not added. In addition, because these components are logically related we develop a Test Suite to group them. Test Suite Description ArrayTestSuite It groups both classes NumberArrayTest and NumberRangeArrayTest. Diego Sanchez Schenone 3 Technologies for Business - Article Unit Tests and IBM Cloud DevOps The JUnit code We are going to take a look at this code fragment to see the main concepts of JUnit which were implemented. package com.tfb.learning.junit.test1; import org.junit.Test; import org.junit.After; import org.junit.Before; import static org.junit.Assert.assertEquals; import com.tfb.learning.junit.app.NumberArray; public class NumberArrayTest { @Before public void setInitialState() { System.out.println("1 - Setting the array to empty"); NumberArray.getInstance().removeAllElements(); } @After public void setFinalState() { System.out.println("After executing all tests"); } @Test public void testIsEmpty() { NumberArray.getInstance().removeAllElements(); assertEquals(true,NumberArray.getInstance().isEmpty()); …. } Sometimes we need to be sure that several conditions should be met before the test execution. In this example, before the test begins, we set the array to empty. JUnit provides the annotation @Before which indicates that a given method is the first to be run. The same is when all tests are done by using the annotation @After. At the end, after all test were executed the method with this annotation is run. By using the annotation @Test we indicates that a given method is a test. This annotation is interpreted by the Junit framework at the moment of the running. Diego Sanchez Schenone 4 Technologies for Business - Article Unit Tests and IBM Cloud DevOps To verify if the result of a method is the expected Junit provides lots of static methods in the class Assert. In the method testIsEmpty() it was used the following assertEquals(true,NumberArray.getInstance().isEmpty()); to check if the result of NumberArray.getInstance().isEmpty() is true. We have to use these static methods to corroborate the test results due to they are part of the framework. The following is the suite which contains the tests for both classes. package com.tfb.learning.junit.test1; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ NumberArrayTest.class, NumberRangeArrayTest.class }) public class ArrayTestSuite { } As we can see by using the annotation @SuiteClasses we declare which classes are part of the suite. Finally, the annotation @RunWith(Suite.class) indicates which class is going to run all test classes. The org.junit.runners.Suite class is provided by the Junit Framework The source code The entire project can be downloaded from Bitbucket at https://[email protected]/dmsanchezschenone/public-tfb-learning-junit.git It was used Maven to develop the Web application that contains these methods. To compile and execute the tests on the components the following commands must be run: mvn compile war:war mvn test Diego Sanchez Schenone 5 Technologies for Business - Article Unit Tests and IBM Cloud DevOps From https://maven.apache.org/download.cgi you can download the lasted version of the framework. Part 3: DevOps and IBM Cloud What is DevOps? As far as I'm concerned the software development process consists of lots of tasks that are closely related to get a common goal (the link that bring all them together).