Testing in DevOps (Test Driven Development)


Reading time: 30 minutes

Test driven development is an approach to development in which the test cases are written first and followed by code just enough pass the test cases developed.

TDD

DevOps emphasizes automating as much as you can.

Automation ensures continuous delivery. Achieving the CAMS formula is a means to achieve continuous delivery. Continuous delivery largely depends on testing, as testing helps in delivering bug-free, reliable software to the customers. Test- driven development is one of the ways of developing and delivering quality software that makes use of automation.

Test driven Development is an approach to development in which automated test cases are written first to test the desired improvement or a new function. Then minimal code is written to pass the test. Once the test gets passed, new tests are written when new functionalities are added. The code is then refactored to acceptable standards. Thus, TDD follows a very short, repeated development cycles.

The steps involved in TDD are:

  • Write a test
  • Run all tests and see if the new one fails
  • Write minimal code
  • Run tests
  • Refactor code
  • Repeat

TDD: Categories of Tests

There are 5 types of tests in Test Driven Development:

  • Unit tests

  • Integration tests

  • Functional tests

  • Regression tests

  • End to end tests

  • Unit Tests:- A smallest part of the code (Often a class or its method) is tested with substitutes such as mock objects or method stubs.

  • Integration Tests:- The Individual modules of software are combined together and tested as a group.

  • Functional Tests:- A quality assurance test that evaluates the overall functionality of the code and compares the actual results with expected results.

  • Regression Test:- It ensures the new changes made to the code have not created any faults.

  • End-To-End Tests:- It is an overall test that includes Unit tests, Functional tests and Integration tests etc

Benefits of Test-Driven Development are modularized, flexible and extensible code, no unnecessary code which saves a lot of time, a minimal working code in shorter time and then test cases to be extended to cover the edge cases and greater confidence in the code.

Different types of testings are used in each stage of development. Some of the methods of tests are
unit tests, integration tests, functional tests, regression test and end to end tests.

Unit test cases are used for testing classes or methods while functional test is used for testing the functionality of the code and tested against the actual expectation.

Automated Test - Specification and Verification Merged

MDD

Combination of Behavior Driven Development (BDD) and Test Driven Development (TDD) principles. Test automation can be adopted in the software delivery process. Combining Behavior Driven Development (BDD) and Test Driven Development (TDD) principles, testing becomes a first class citizen in the process and techniques replace traditional Waterfall process artifacts.
The process:

Given an Agile development process, the development cycle starts with a backlog of user stories.

These user stories are refined with clear acceptance criteria.

  • Now, a feature test can be specified using the BDD “Specification By Example” technique. This technique defines functionality using a strict syntax (Gherkin specification). The specified functionality (for example, the feature test scenarios) proves the acceptance criteria of the user story. The feature test specification is also a functional specification of the system.

  • Now, when the feature test is executed, it will fail, because at this point, no code has been written to implement the user story.

  • Next, the low level tests, like unit tests can be specified. Specifying these types of test is both a test specification, as well as a (detailed) design activity. Writing these type of tests upfront forces you to think about the (planned) software design.

  • When these low level tests are executed immediately, they will fail, because at this point, no code has been written to implement the user story.

  • Now, the software for (part of) the user story can be written.

  • The implemented software can be tested with the specified low level tests. The goal is to ensure that all low level tests run successfully. This includes fixing bugs and refactoring the software code and/or test code.

  • Once, all low level tests are successful, the feature tests can be executed. If these tests fail, the process of writing low level tests, adjusting the software code, fixing bugs, and refactoring is repeated until all feature tests and low level tests run successfully.

Types of Automated Tests

Automated tests are generally divided into multiple ‘suites’. Different suites of automated tests are depicted in the illustration below.

  • Unit tests: Unit tests are run first by developers, in some cases even before they add their changes to the repository. Unit tests normally test individual classes or functions. When those classes or functions need access to external resources, those resources are often faked as “mocks” or “stubs”.

  • Integration tests: Integration tests are a next level up from unit tests. Integration tests make sure that the modules that comprise an application work properly with each other. Ideally integration tests are run in environments that are similar to the production environment. For example, you’d want to make sure that if your application uses a database that the same database is available in your integration environment.

  • System tests: Should test the entire system in an environment as close as possible to the real production environment.

Testing Tools

Some common testing tools in DevOps are:

  • Jenkins
  • Gradle
  • Selenium

Jenkins:-
This is a powerful apparatus for DevOps engineers trying to support a CI/CD pipeline. Jenkins inserts with Git source control archives, for example, GitHub and licenses you basically to computerize assemble, bring the code and continuously offer new facilities.

Features are as follows:-

  • It can distribute errands through different machines in this way enhancing simultaneousness.

  • Jenkins requires little upkeep and has an inalienable GUI gadget for calm updates.

  • It improves your foundation's degree of mechanization.

  • Jenkins gives 400 plugins to help with creating and breaking down for all intents and purposes for any undertaking.

  • It tends to be effectively set up and composed by means of the web interface.

Gradle:-
Since 2009, Gradle has been around as a substitute to Maven and Apache Ant. Gradle is a structural device that licenses clients to code in Python, Java, and C++ alongside different dialects.

Features are as follows:-

  • Gradle is evaluated to be multiple times quicker than Maven in processing. The improvement in speed is because of Gradle's daemon and because it has further developed cache.

  • Experts have launched a Kotlin-powered DSL for customers who would prefer to keep away from the expectation to learn and adapt to Groovy.

  • Further, GWorkspace ought to be acquainted with Maven handlers.

Selenium
Selenium is used in a testing phase. It tests web applications and provides an interface for developing an automated test. it is also a free Open-source tool. we can write script in various languages for example- JAVA, Python, Javascript, etc. It is easy to build a keyword-driven framework for a web driver.

Selenium is not just a single tool but a suite of software, each catering to different testing needs of an organization. It has four components:

  • Selenium Integrated Development Environment (IDE)
  • Selenium Remote Control (RC)
  • WebDriver
  • Selenium Grid

With this, you have the complete knowledge of testing and test driven development (TDD) in DevOps. Enjoy.