DEV Community

Gaurav Singh
Gaurav Singh

Posted on • Originally published at automationhacks.io on

How to work with redis for test automation

Redis - Wikipedia
Source: https://en.wikipedia.org/wiki/Redis

Redis is an in-memory data structure store which can be used as a database cache and supports different data types, most notably a simple key, value pair (aka dict), lists, etc.

It could be used for any relevant purpose within your test automation framework, for instance, one of the use cases that I found helpful was that we could use it as a cache of temporary auth information for test user accounts , which could be retrieved by tests running in parallel and avoid calling the actual auth APIs every time, Thus cutting a bit of external dependencies and making your tests faster.

Redis provides a server and a *CLI * interface if you want to play with the data and a bunch of client libraries for different programming languages to use them in your solution

In this post, we would see how to set up redis on an existing Ubuntu VM and how to interact with it for a very simple use case via redis-cli and then see what a simple kotlin file could look like to have an abstraction over this.

Installation and setup of redis server on an Ubuntu VM

Pre-requisites:

  • Ensure you have ubuntu already setup on your machine or have a VM created.
  • SSH into the VM or open terminal app
  • Execute sudo suto elevate your permissions to root
  • Ensure you have the GCC compiler already installed, if not, follow the below steps to set it up.
sudo apt update
sudo apt install build-essential
gcc --version
Enter fullscreen mode Exit fullscreen mode

Install Redis via sudo apt

sudo apt update
sudo apt install redis-server
Enter fullscreen mode Exit fullscreen mode

Expose the redis server to outside the VM

By default, redis would start running on localhost (127.0.0.1) , however, if you want to use it for your use cases, you would need to update the IP which it binds to in redis.conf file

Open the file in your comfortable editor of choice

sudo vim /etc/redis/redis.conf
Enter fullscreen mode Exit fullscreen mode

Inside redis.conf file, update

bind 127.0.0.1
Enter fullscreen mode Exit fullscreen mode

to

bind 0.0.0.0
Enter fullscreen mode Exit fullscreen mode

Restart redis service

sudo systemctl restart redis.service
Enter fullscreen mode Exit fullscreen mode

Once done, check if the redis service is running

sudo systemctl status redis
Enter fullscreen mode Exit fullscreen mode

You should see an output like below:

redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since ... ;machines\_local\_timezone...;; 4min 15s ago......
Enter fullscreen mode Exit fullscreen mode

Note: I have intentionally removed some part of this output as it held sensitive information, However, you should see details around your VM/machines hostname and CPU/memory stats for your current redis instance.

Cool, πŸ₯³ with this we have already set up a *redis server * on Ubuntu.

πŸ€ΎπŸ»β€β™‚οΈ Play with redis-cli

To open redis-cli just type below

redis-cli
Enter fullscreen mode Exit fullscreen mode

We can create a dummy key and set its value via the set command and get its value.

127.0.0.1:6379> set "test-key" "test-value"
OK
127.0.0.1:6379> get "test-key"
"test-value"
Enter fullscreen mode Exit fullscreen mode

And that’s it.

If you want to connect to this from your local machine or runner, enter below command (provided you have redis-cli already setup)

redis-cli -h host\_name\_or\_ip -p 6379
Enter fullscreen mode Exit fullscreen mode

How do we interact with this in our automation code?

Redis supports multiple clients to interact with the redis server. In below example, we have chosen to use Jedis, however based on your needs you could go with any other client as well.

Add below line in your build.gradle file

compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
Enter fullscreen mode Exit fullscreen mode

In below class, we create a class RedisHandler which accepts a host and port (where your redis server is running) and then has simple get(key) and put(key, value) methods to work with redis (quite similar to how we used the cli above)

Simple Kotlin file with an abstraction over jedis (JVM support library for redis)

And that’s a wrap πŸ₯³. Hopefully you would find creative use cases to use redis augment your automated tests. If you found this useful, do share with a friend or colleague. Until next time. Happy testing and automating!

Related articles

Further read on Redis, its data types and cli utility:

Top comments (0)