11. Tests
Tags: aws, python, testing
Published: 2021-02-12 02:59:02

To test the web application for my [website](https://www.komlalebu.com), I had to set up [pytest](https://realpython.com/pytest-python-testing/) for testing my code locally as well as during deployment in Github actions. 

Git: https://github.com/jicoing/git-komlalebu-sam

![Alt Text](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2FSVaLSBSWB0zTjkp7md%2Fgiphy.gif&f=1&nofb=1)

# Pytest Setup

> * Create virtual environmnet - virtenv

1 - For PC I had to change the execution policy from `powershell`.

               Set-ExecutionPolicy Unrestricted -Force

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nglt6vadjztz18sx7idw.PNG)

2 - Then ran the below commands in my project folder to setup the virtual environment.
>
                $ mkdir virtenv
                $ cd virtenv
                $ python -m venv .env

This will create a virtual environment `.env` in our project's folder `virtenv`.

# Structure

git-komlalebu-sam
 ┣ 📂komla_function
 ┃ ┃
 ┃ ┣ 📜app.py
 ┃ ┗ 📜__init__.py
 ┣ 📂tests
 ┃ ┃ 
 ┃ ┣ 📜test_handler.py
 ┃ ┗ 📜__init__.py
 ┗ 📂virtenv
 ┃  ┃ 
 ┃  ┗ 📂.env
 ┃  ┃     ┃
 ┃  ┃     ┣📂Include
 ┃  ┃     ┣📂Lib
 ┃  ┃     ┗📂Sripts

3 - Next, I had to activate this virtual environment using the following command:

>
               $ .env\scripts\activate

>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hk1q93olq3rinepkbxol.PNG)

4 - Then I had to install pytest in the virtual environment.
>       
               $ pip install pytest

>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s0m3fk4sfd4nrl4e6a5c.PNG)

5 - For the tests to run I had to make sure that `_init_.py` files are present in my `📂komla_function` folder  which has the lambda handler: `app.py`, as well `📂tests` folder which has the test handler: `test_handler.py` to fix module-not-found error.

>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q7n0hirwhn9oij54v0km.PNG)

# Test_handler.py
* The test handler `Test_handler.py` basically checks if the below string is present in my lambda handler `app.py`.

>
                       'statusCode': 200 

> `Test_handler.py`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jq8kk7p6t60gv3zyer29.PNG)

> `app.py`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/649ci5aurb6xfwdrlghn.PNG)

6 - Once the set up was done I fired up pytest and got the expected result: `Pass`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rr2wk8hkrxzdinjd035z.png)

Upon altering the status code in `app.py` the test fails.

`Fail`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3jy7pa7m8hmeht38hjas.PNG)

# Requirements

7 - Finally I created the requiremenst.txt once pytest was done. This file is needed for executing the tests in virtual environment created by the dockerfile in Github actions,

>
              $ pip freeze > requirements.txt

>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/djgfirnkhqb3ohx3pahq.PNG)

#Final Tree:

git-komlalebu-sam
 ┣ 📂.github
 ┃ ┣ 📂actions
 ┃ ┃ ┗ 📂sam
 ┃ ┃ ┃ ┗ 📂package
 ┃ ┃ ┃ ┃ ┣ 📜action.yml
 ┃ ┃ ┃ ┃ ┣ 📜Dockerfile
 ┃ ┃ ┃ ┃ ┗ 📜entrypoint.sh
 ┃ ┗ 📂workflows
 ┃ ┃ ┗ 📜ci.yml
 ┣ 📂komla_function
 ┃ ┣ 📜app.py
 ┃ ┗ 📜__init__.py
 ┣ 📂tests
 ┃ ┣ 📜test_handler.py
 ┃ ┗ 📜__init__.py
 ┣ 📜README.md
 ┣ 📜requirements.txt    <--------------`requirements`
 ┗ 📜template.yaml

# Github Actions

* I setup ci.yml as below. This workflow is named "Deploy SAM Stack to Production". it will be started when pushing code from the master branch of our repository. It contains one job named `test` with four steps which will run inside a Ubuntu runner.

>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zc4t9shv1nefwuj95xdl.PNG)

We first give the workflow access to the code of the repository using the checkout@v2 action. Next, we add a step named Install Python 3 which makes use of the setup-python@v1 action to install Python 3.6. Next, we add a step that will install the dependencies of our project in Ubuntu. Finally we add a step for running our tests using pytest.

On running the following commands to commit and push to GitHub repository, tests automatically run:

                        $ git add -A
                        $ git commit -m "First commit"
                        $ git push origin master 


Once the tests complete my SAM stack gets deployed as per  `ci.yaml`.
The action has two jobs test and deploy. `test` - checks the integrity of my lambda function. `deploy` - deploys the entire SAM stack in AWS and needs test to be successful as a dependency.([read more](https://dev.to/jicoing/14-ci-cd-back-end-2eid))

            jobs:
              test:
              delpoy:
                    needs:test

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jlowwgfdsdoiu3emt623.PNG)

And the testing was setup.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0vobgcej8t0ytnl5d9l4.gif)