Python Test Automation Project Dockrize with CI/CD
Published: 2022-12-11 11:23:05

Part - 01 - https://dev.to/devsk1207/api-testing-using-python-introduction-configuration-and-installation-of-the-python-3b45
Part 02 - https://dev.to/devsk1207/read-and-write-data-from-google-sheets-using-python-for-generate-api-test-automation-report-2co5

Github - https://github.com/DevSK1207/DarazFlexPythonProject


**<u>Create DockerFile</u>**

```
FROM python:3.8

# Install pip for install, manage and update software packages written in Python
# The curl command is a tool for transferring data over the network, using a variety of protocols.
# In this case, the curl command is being used to download the get-pip.py script from the PyPI website using the HTTP protocol
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
RUN python3 get-pip.py

# Check Pip Version
RUN pip3 --version

# Install requests which we would use to actually make HTTP requests
RUN python3 -m pip install requests

# Install jsonpath to extract or manipulate data from JSON
RUN pip3 install jsonpath

# Install pytest and any other dependencies
RUN pip install pytest

# Install pytest that generates a HTML report for test results
RUN pip3 install -U requests Flask pytest pytest-html

# Install nltk for python 3
RUN pip3 install nltk

# Import nltk
# NLTK is a toolkit build for working with NLP in Python.
# It provides us various text processing libraries with a lot of test datasets
RUN python3 -c "import nltk; nltk.download('all')"

# Install Java for include additional instructions for installing any dependencies that are required by Allure
RUN apt-get update && apt-get install default-jre -y

# Install Allure
RUN apt-get update && apt-get install allure -y

# Install gspread and any other required dependencies
RUN pip3 install gspread

# Install oauth2client and any other required dependencies
RUN pip3 install oauth2client

# Copy the current directory (which should contain Test Automation bin) into the container
COPY . /pythonProject

# Set the working directory to the app directory
WORKDIR /pythonProject

# Run freeze command is a tool that is used to generate a requirements file
# Install requirements text file to saves a list of the modules and packages required by your project
RUN pip3 freeze
RUN pip3 freeze > requirements.txt
RUN pip3 install -r requirements.txt

# Run this script to make shell file executable.
RUN chmod u+x bin/docker.sh
RUN chmod u+x bin/deploy.sh
RUN chmod u+x bin/run.sh

# Run pytest to run your tests
CMD ["pytest"]
```

**Build Docker Image**

To run a Dockerfile, you first need to build a Docker image from the Dockerfile using the docker build command. This command takes the Dockerfile as input and creates a Docker image that includes all the instructions and dependencies specified in the Dockerfile.

Here's an example of how to build a Docker image from a Dockerfile:

```
# Build the Docker image
docker build -t myimage .
```

**RUN Docker Container**

To run a new Docker container based on an image and enter the container's shell, you can use the docker run command with the -it flag, followed by the name or ID of the image. This will start a new container based on the image and open an interactive shell inside the container, allowing you to run commands and interact with the container.

Here's an example of how to use the docker run command to start a new container and enter its shell: 

```
docker run -it myimage /bin/bash
```
This command will start a new container based on the "myimage" Docker image, open an interactive shell inside the container, and run the /bin/bash command. This will give you a shell prompt inside the container, where you can run commands and interact with the container.