Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
MinGi-K authored Nov 20, 2023
2 parents 690b9c2 + c27e3ec commit 84d415b
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Topics/Development_Process.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
### [Trunk-Based Development](./Development_Process/Trunk_Development.md)
### [Django Project Deployment: AWS, Vercel, and Railway](./Development_Process/Django_Deployment_AWS_Railway_Vercel.md)
### [Automated Frontend Deployment with Vercel](./Development_Process/Frontend_Automated_Deployment_Vercel.md)
### [Flask Application Deployment on Heroku](./Development_Process/Flask_App_Deployment_Heroku.md)
### [Quality Assurance Testing](./Development_Process/QA_testing.md)
- [Automated Testing](./Development_Process/Automated_Testing.md)

- [Large Language Model (LLM) for Testing and Debugging](./Development_Process/LLM_Testing_Debugging.md)
### [Getting Started With Docker](./Development_Process/Docker.md)
### [Getting Started With WSL 2](./Development_Process/WSL.md)

Expand Down
22 changes: 21 additions & 1 deletion Topics/Development_Process/Automated_Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,24 @@ Integration testing, in contrast to unit testing, is meant to verify that your c
- **Incremental Approach**:
- Educative: [What is incremental testing?](https://www.educative.io/edpresso/what-is-incremental-testing)
- **Sandwich/Hybrid Approach**:
- Educative: [What is the hybrid testing approach?](https://www.educative.io/answers/what-is-hybrid-integration-testing)
- Educative: [What is the hybrid testing approach?](https://www.educative.io/answers/what-is-hybrid-integration-testing)

#### API Testing

Another important area of automated testing is API, or Application Program Interface, testing. API testing can be helpful to integrate into your automated tests as it ensures that the requests that your software sends are correctly received and in turn, you receive the right output. API testing is incredibly powerful and can be used to not only check that the right status is received but also to check that the data returned is of a particular form or has certain attributes, amongst other things. Errors returned in automated API tests can be a sign that there is an error in your code in the requests you send or that there has been a change to the API that you are accessing. Either way, these tests can be a great method of catching these issues early and resolving them before your code is deployed. In larger software systems, there are actually a number of applications of API testing beyond the two mentioned above which can include aspects such as penetration testing or security testing and a more extensive overview of these kinds of tests can be found at [https://blog.hubspot.com/website/api-testing](https://blog.hubspot.com/website/api-testing).

##### Testing in Postman

A common tool for testing APIs during the development phase is Postman, which allows you to send a variety of requests such as GET and POST requests and examine status codes and outputs. However, Postman can actually also be used to create automated API tests that can be integrated with your CI/CD pipeline. These automated tests can then be configured to run on certain actions, such as a push to main. In order to understand how to set up this automated testing, we first need to understand how testing works in Postman. To automate your API tests and integrate them into your CI/CD pipeline, Postman requires that your tests be capable of being run through the command line interface, or CLI. This, however, is not the only way that Postman supports testing; rather, there are 3 methods:

1. Manually: After setting up your Postman tests, you can run them manually through the application.
2. Scheduled: You can schedule your tests to be run at regular intervals from the Postman cloud, as determined by you (e.g. once a day, once a month, etc.).
3. Through the CLI: After setting up your tests in Postman, you can run them through a command line interface such as Terminal by generating an API key that lets you log in to Postman from your Terminal and then execute a command to run your collection. This command is provided to you by Postman.

##### Setting up Automated Testing in Postman

Having understood the 3 methods of testing in Postman, I will now delve into the last one in more detail to allow you to set up automated API testing. In the Postman app, you should navigate to "APIs" in the left sidebar. Then, create a new API by pressing the + sign and give it an informative name. Inside this API you want to create a new request for every endpoint or feature that you want to test. This can be done by right-clicking and selecting "Add request". Inside the request, add any necessary parameters or headers as you normally would inside Postman. Then, you can click on the "Tests" tab and create your own Postman tests here. To learn more about the syntax for doing so you can use the quick help feature which describes how to write these tests in more detail. To set up automated testing you can then right-click your API and select "Run collection". In the sidebar that opens on the right, you will now see the 3 options discussed above. Click on "Automate runs via CLI" (although it can be a good idea to run your tests manually once to confirm they work as a sanity check) and then press "Configure command" under the "Run on CI/CD" header. This will open a new page that allows you to select your CI/CD provider and the operating system environment for the pipeline. You can copy and paste the generated commands into your configuration file. Make sure you generate an API key and add it to your repository's secrets. For reference on how to do this in GitHub, see [https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions). You now have automated API testing set up through the Postman CLI!

##### Postman Automated Testing Involving Authorization

Note that there are some extra steps that you must take if your API requests require authorization in the form of a bearer token to be properly processed since if you hard-code the token into the request it will eventually expire and cause the automated tests to fail. This can be the case for a lot of APIs but does not mean that you can't incorporate automated API testing. Instead, what you must do is add a request inside your API collection that gets these authorization details. You can then create an environment by clicking on "Environments" in the left sidebar underneath "APIs". Create a new environment and define environment variables that will store all the necessary authorization details from your request. You can now navigate back to your API and select the environment you just created from the dropdown on the top right of your screen. Go to the "Tests" tab of the authorization request you added and add a line of code to scrape the authorization token and other details from the request's output and store it in the environment variables you created. Once again, the Postman quick help contains information on how to write this line of code under "Set an environment variable". Then, for all the other requests in the API, you can add the relevant authorization details by clicking on the "Authorization" tab and filling out the fields using your environment variables. Lastly, you must ensure that when you create your test collection you re-order the tests by dragging and dropping so that the request that collects authorization details is run first, enabling the rest of your tests to work correctly with renewed authorization details. Using refresh tokens or the form of scraping described above is incredibly beneficial for automated API testing as it ensures that you, as the developer, will not have to manually change the tokens of your requests every time you want to commit to a branch or main and have these automated tests run. Rather, you can rest assured that these tests are truly automated now and do not require any manual changes from you in order to be run.
115 changes: 115 additions & 0 deletions Topics/Development_Process/Flask_App_Deployment_Heroku.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Deploying a Flask Application on Heroku

## Table of Contents
### [Introduction](#introduction)
### [Before Getting Started](#before-getting-started)
### [Steps](#steps)
### [Common Issues](#common-issues)

----

## Introduction

This simple guide will walk you through the steps of deploying a Flask web application on Heroku. Heroku is a popular cloud-based platform that allows for the simple deployment and management of web applications.

----

## Before Getting Started

Before you begin, ensure you have the following:

- An account with Heroku. Here is where you can sign up: https://www.heroku.com
- A Git repository that contains your Flask application.
- Heroku CLI installed. Here is where you can download it: https://devcenter.heroku.com/articles/heroku-cli

----

## Steps

**1) Logging into Heroku Through the Terminal:**

Open your terminal and navigate to your Git repository's root directory. Then, type the following into the terminal and follow the instructions to log into Heroku:

heroku login

**2) Creating a requirements.txt File:**

If you don't have one already, add a file into your project's directory named "requirements.txt" that includes all the requirements for your application. The simplest way to create one is by typing the following command:

python3 -m pip freeze > requirements.txt

**3) Installing Gunicorn:**

If you don't already have Gunicorn installed, install it by typing the following command into the terminal:

pip3 install gunicorn

Gunicorn serves as a connection between your Flask app and Heroku's dyno and allows for improved reliability in terms of the deployment of Python applications.

**4) Creating a Procfile:**

Create a file named "Procfile" in your project's root directory. This file is required for Heroku to deploy your application. In your "Procfile", add the following line:

web: gunicorn app:app

**5) Creating a New Heroku Application:**

While in your Git repository's root directory, type the following to create a new app on Heroku using the terminal:

heroku create name-of-your-app

Remember to replace "name-of-your-app" with a name for your Heroku app. This name must be unique across all of Heroku.

**6) Pushing Changes:**

Ensure that all the changes are committed and pushed to your repository.

**7) Deploying the Application:**

You can now deploy your Flask application to Heroku by typing the following command into the terminal:

git push heroku master

**8) Opening the Application:**

You can now open the deployed application in your default browser by typing the following command into the terminal:

heroku open

**Additional Comments:**

If you prefer a more visual approach to deploying your Flask application on Heroku, you could use Heroku's web interface instead of the CLI. While this method is more user-friendly, it has a few drawbacks including limited control over advanced configuration settings and dependency on a web browser.

This method leads to manual deployment through which the command "git push heroku master" triggers the deployment. This gives you full control over what and when to deploy. Alternatively, you could also connect a GitHub repository to your Heroku app through their website to set up automatic deployment. When any changes are pushed to your repository, Heroku will detect them and deploy the app again right away.

----

## Common Issues

**Application Filename is Not "app.py":**

Ensure that your Flask application's filename is either "app.py" and that the Flask application's instance within that file is stored in a variable called "app". Otherwise, you can change your Procfile to reflect the file and variable names you used by changing the line in it to the following:

web: gunicorn your_filename:instance_variable_name

Remember to replace "your_filename" and "instance_variable_name" with the names you used.

**Other Issues with Deployment:**

Firstly, ensure that your "requirements.txt" file is created and is up to date. Also, ensure that your Procfile is free of any typos.
If you still encounter any issues with deployment, checking the Heroku logs created can be very helpful. To view real-time logs, type the following into the terminal:

heroku logs --tail

Some logs Heroku provides include:

- Error logs which contain error messages caused from the execution of your application's code
- Application logs which contain information about the execution of your application's code including print statements
- Router logs which contain information about incoming traffic and issues with request handling.

----

For More Detailed Steps for the Creation and Deployment of a New Flask Application:
- https://realpython.com/flask-by-example-part-1-project-setup/


Loading

0 comments on commit 84d415b

Please sign in to comment.