How to work with redis for test automation
Tags: frameworkdevelopmen, redis, testautomation, ubuntu
Published: 2020-05-30 02:19:39

---
title: How to work with redis for test automation
published: true
date: 2020-05-30 02:12:15 UTC
canonical_url: https://automationhacks.io/2020/05/30/how-to-work-with-redis-for-test-automation/
---

![Redis - Wikipedia](https://upload.wikimedia.org/wikipedia/en/thumb/6/6b/Redis_Logo.svg/1200px-Redis_Logo.svg.png)<figcaption>Source: <a href="https://en.wikipedia.org/wiki/Redis">https://en.wikipedia.org/wiki/Redis</a></figcaption>

[Redis](https://redis.io/) is an in-memory data structure store which can be used as a database cache and supports different [data types](https://redis.io/topics/data-types-intro), 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](https://redis.io/clients) 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 su`to elevate your permissions to root
- Ensure you have the [GCC compiler](https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/) already installed, if not, follow the below steps to set it up.

```
sudo apt update
sudo apt install build-essential
gcc --version
```

### Install Redis via sudo apt

```
sudo apt update
sudo apt install redis-server
```

### 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
```

Inside `redis.conf` file, update

```
bind 127.0.0.1
```

to

```
bind 0.0.0.0
```

### Restart redis service

```
sudo systemctl restart redis.service
```

Once done, check if the redis service is running

```
sudo systemctl status redis
```

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......
```

> 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, ![🥳](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f973.png) with this we have already set up a **redis server ** on Ubuntu.

## ![🤾🏻‍♂️](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f93e-1f3fb-200d-2642-fe0f.png) Play with redis-cli

To open redis-cli just type below

```
redis-cli
```

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"
```

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
```

## 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'
```

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)

{% gist https://gist.github.com/gaurav-singh/38a345e245585904f27b9d5f67377e5f %}

<figcaption>Simple Kotlin file with an abstraction over jedis (JVM support library for redis)</figcaption>

And that’s a wrap ![🥳](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f973.png). 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

- [How to install Redis on ubuntu](https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-redis-on-ubuntu-18-04)
- [How to open Redis port for remote connections](https://stackoverflow.com/questions/19091087/open-redis-port-for-remote-connections)

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

- [Redis quickstart](https://redis.io/topics/quickstart)
- [Redis data types](https://redis.io/topics/data-types-intro)
- [Redis client libraries](https://redis.io/clients#java)