DEV Community

loading...
Cover image for 11. Tests

11. Tests

jicoing profile image jicoing ・3 min read

To test the web application for my website, I had to set up pytest for testing my code locally as well as during deployment in Github actions.

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

Alt Text

Pytest Setup

  • Create virtual environmnet - virtenv

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

           Set-ExecutionPolicy Unrestricted -Force
Enter fullscreen mode Exit fullscreen mode

Alt Text

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

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

           $ pip install pytest

Alt Text

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

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

app.py
Alt Text

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

Alt Text

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

Fail
Alt Text

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

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

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 
Enter fullscreen mode Exit fullscreen mode

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)

        jobs:
          test:
          delpoy:
                needs:test
Enter fullscreen mode Exit fullscreen mode

Alt Text

And the testing was setup.

Alt Text

Discussion (0)

pic
Editor guide