Docker Image of your Testing Project / Dockerise test execution

arvind_choudhary

Arvind Choudhary

Posted on November 3, 2022

Docker Image of your Testing Project / Dockerise test execution

Docker image


I previously wrote about docker & testng, how it can be useful? but did you ever feel that why should we keep running tests in complex manner( on local machine/on virtual machine)? Can we make it simple enough for everyone to run tests with ease?!

What if we have a docker-image of our test code which when executed will test our app. you just have to do docker run <image-name> and that's it. your scripts shall trigger inside container and on completion you should be able to get test status on console!

Few advantages:

  1. Anyone can execute scripts & get test results.
  2. Development/Testing knowledge is not required.
  3. Test execution is completely isolated.

Github repo: Arvind142/TestNG-Modular-Automation-Framework


Start with creating test suite, verifying POM!

testng.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<!-- Listeners to help with reporting -->
<listeners>
<!-- Listener Class declaration -->
</listeners>
<test name="Test">
<packages>
<!-- test package declaration -->
<package name="com.core.test_package.*"></package>
</packages>
</test> <!-- Test -->
</suite> <!-- Suite -->

Enter fullscreen mode Exit fullscreen mode




Pom.xml




<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

Enter fullscreen mode Exit fullscreen mode




Trigger them to Verify it's working.

mvn clean test -Dsurefire.suiteXmlFiles=/path/to/testng.xml


Create image of your code

Dockerfile


Base image

FROM maven:3.8.5-openjdk-11-slim

Argument with default value

ARG suiteFile=testng.xml

Copy code from local to image

COPY ./ /app

Specify working directory

WORKDIR /app

Execute command at creation of image

RUN mvn clean compile

Command to be executed at start of container

CMD mvn test -Dsurefire.suiteXmlFiles=$suiteFile -e

Enter fullscreen mode Exit fullscreen mode




Build your image

docker build -t arvindc142/framework:v1 .

Build SS


Run you image

docker run arvindc142/framework:v1

Run different test suite (other than testng.xml, which was metioned as default suite)

docker run -e suiteFile=testng-karate.xml arvindc142/framework:v1

Remember to publish image to image repo so that it is available for others to leverage.

docker image push arvindc142/framework:v1


What about test reports?

since tests were executed in container then reports are within container, how can i get them? Volume Mapping

Volume Mapping

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.

Note: In my case reports are generated in test-output folder, I’ll map test-output of my image with my machine’s folder (i.e., test-results) so that I can get test result there! (Use PowerShell on windows)

docker run -v $PWD/test-results:/app/test-output arvindc142/framework:v1

Before Snapshot:

Before SS

After executing docker run with -v option:

After ss

RPT


BONUS!!!

Let’s add selenium grid with code for UI test script execution.

above setup were only for test execution it didn’t cover selenium-based UI script execution.

Note: Remote url will be http://selenium-hub:4444/wd/hub inside framework container.

Container can connect to hub container with host as selenium-hub, i’ve also exposed port 4444 to watch script execution from your machine. you can access hub with http://localhost:4444/ui

Hub Snapshot:

grid

Docker-compose for Selenium grid and our test script execution



version: '3.7'

compose version details

services:
#-SELENIUM GRID SETUP FOR AUTOMATION EXECUTION-
selenium-hub:
image: selenium/hub:latest
container_name: selenium-hub
ports:
- 4444:4444
chrome:
image: selenium/node-chrome:latest
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
edge:
image: selenium/node-edge:latest
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
firefox:
image: selenium/node-firefox:latest
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
#-SELENIUM GRID services ended-

#-Framework image-
# service name(container name)
modular-automation-framework:
# which container to use for building
image: arvindc142/framework:v1
# to copy project into container
volumes:
- './test-results/:/app/test-output/'

Enter fullscreen mode Exit fullscreen mode




To spin up all containers

docker-compose up -d

spin up

Get all containers

docker ps -a

Print container logs on console

you can get container-id’s with docker ps command
docker logs -f <container-id>

To stop & remove containers

docker-compose down

Close DOwn


References

Run TestNG from commandline

Github TestNG Project

Volume Mapping

Docker-Selenium

docker hub arvindc142/framework

💖 💪 🙅 🚩
arvind_choudhary
Arvind Choudhary

Posted on November 3, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related