Unit Testing vs Integration Testing. JMeter to the rescue.

The importance of Unit Testing in software development is not ignored anymore. Almost each programming language in backend & frontend projects has its own unit testing frameworks. Nowadays software developers use unit testing mainly as a TDD tool to prove their understanding & correctness of program basic building blocks — classes, component’s & units. Test-Driven Development is a new architectural style. Even if you are not an experienced software developer using unit testing will force you to develop in a more abstract, encapsulated & low coupling way. Usually, it leads to better reusability, increases the courage to code refactoring & architectural improvements. Unit testing is also one of the key components which enables Continuous Integration in the projects.

But enough about unit testing. It is often not well understood by the business people & customers. If your project consists of several structural components like databases, web services, JavaScript frontend, or mobile clients they all must communicate correctly & effectively. That is where developers are focusing on interfaces, protocols, and communication between components to make the whole system alive. And like with unit testing, integration testing should be used to prove that communication between components is established in an expected way. Integration testing makes one further step from Continuous Integration to Continuous Delivery. To make the delivery process effective the project plan must include continuous QA processes, both manual & automation.

By writing good integration tests developers show to QA & business people that they care about quality. So, before committing & merging their code to release branches unit & integration tests are executed by continuous integration pipeline tools. That enables rapid development, often commits, and an error-free continuous deployment pipeline.

So, you probably already noticed the functional difference between unit & integration testing. They focused on testing different levels of software, and they differ in methodologies and execution manner. Unit tests care only about a particular atomic software unit. All external dependencies are mocked. One very important side effect of mocking is fast tests execution which is really important in the CI pipeline. You don’t need to wait dozens of minutes or even hours to run all tests and prove that your commit will not break the development environment for the whole team. You also don’t produce unnecessary application load to the database, monitoring & logging systems upon each code commit.

On the contrary to that, Integration tests are slow and heavyweight. Despite they are using often the same frameworks as unit tests (like JUnit), their nature is different. They require network communications between services or real database connections. They are focused on many functional components or application layers to check that system components could work as a whole and interfaces work according to contracts. Usually, we don’t need to test all possible application scenarios with integration tests (it is the job of unit tests to keep high code coverage and test most of the use cases, error scenarios, and corner cases). Writing integration tests we must focus on interfaces, configurations & communication between components. Since often integration tests are really slow, you probably shouldn’t execute them as part of the CI but instead, include them as part of the CD pipeline, which usually is executed not so often (during the night or weekly builds).

JMeter For Integration & Performance Testing

Sometimes integration testing is too expensive because it requires significant development efforts. In the case of REST services testing, you can utilize tools like JMeter to implement integration tests faster and even by non-developers. JMeter was designed as a load testing tool because it has a lot of features to simulate the load of thousands of users, threads, sessions and collect a statistic about system performance. Despite that JMeter is also very convenient to implement integration tests or system sanity tests for REST services. Also, JMeter could be considered as an alternative to the popular Postman API test client. Postman is very popular because of its nice UI, but it lacks such flexibility that JMeter has. JMeter could be used by non-developers thanks to its build-in JSON, XML & XPath parsers as well as popular among Java developers thanks to its pure Java nature and fast scripting using Java or Scala or Groovy syntax.

Amazon Cognito Login & OAuth 2.0 Services Testing

Recently I implemented one service that required Amazon Cognito OAuth 2.0 authentication to login. Thanks to JMeter I quickly simulated user login & password submission to get JWT access token and test all my APIs that require authentication. Unfortunately with Postman, I had no such possibility because Postman OAuth 2.0 authentication plugin requires manual input of username & password to get the access token.

You can download my JMeter project from GitHub https://github.com/vlytsus/jmeter_cognito_testsuite and easily configure to use with any REST service that requires automatic user login & authentication. The project has 4 configuration profiles for DEV/QA/STAGE and PROD accounts. You can easily enable/disable the required execution profile, create huge groups of tests, use context variables to parse responses of one service, and pass them to the next requests to build chains of logically related business flows. Once I even used JMeter script to register thousands of new users using CSV file with emails & passwords.

Another big advantage of using JMeter is that you can turn it into the load or automation testing tool at any moment by running it in the command line mode. Ultimately you can execute JMeter tests as part of your CI/CD pipeline thanks to Maven & Gradle plugins.

Please check my JMeter test suite project on the GitHub repo: https://github.com/vlytsus/jmeter_cognito_testsuite

Software Developer & Team Leader