Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Writing Tests in Flask


Introduction

Testing is a critical part of software development that ensures the reliability of your application. Flask provides tools for writing unit tests for views, models, and forms, as well as mocking database interactions.

In this article, we will demonstrate step-by-step how to write tests in Flask using real examples.

Step 1: Setting Up the Project

First, create a Flask project and install the required dependencies:

            pip install flask
            pip install flask-sqlalchemy
            pip install pytest
        

Create a basic Flask app with a database:

            from flask import Flask, jsonify
            from flask_sqlalchemy import SQLAlchemy

            app = Flask(__name__)
            app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
            app.config['TESTING'] = True
            db = SQLAlchemy(app)

            class Item(db.Model):
                id = db.Column(db.Integer, primary_key=True)
                name = db.Column(db.String(100))

            db.create_all()

            @app.route('/items')
            def get_items():
                items = Item.query.all()
                return jsonify([{'id': item.id, 'name': item.name} for item in items])

            @app.route('/add_item/', methods=['POST'])
            def add_item(name):
                item = Item(name=name)
                db.session.add(item)
                db.session.commit()
                return jsonify({'id': item.id, 'name': item.name})
        

Step 2: Writing Unit Tests for Views

Create a test file named test_app.py. Use Flask's test client to simulate HTTP requests:

            import pytest
            from app import app, db, Item

            @pytest.fixture
            def client():
                app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
                with app.test_client() as client:
                    with app.app_context():
                        db.create_all()
                    yield client

            def test_get_items_empty(client):
                response = client.get('/items')
                assert response.status_code == 200
                assert response.json == []

            def test_add_item(client):
                response = client.post('/add_item/TestItem')
                assert response.status_code == 200
                assert response.json['name'] == 'TestItem'

            def test_get_items(client):
                client.post('/add_item/TestItem')
                response = client.get('/items')
                assert response.status_code == 200
                assert len(response.json) == 1
        

These tests verify that the endpoints work correctly when adding and retrieving items.

Step 3: Writing Unit Tests for Models

Write tests to validate the behavior of your database models:

            def test_item_creation(client):
                with app.app_context():
                    item = Item(name='TestItem')
                    db.session.add(item)
                    db.session.commit()
                    assert item.id is not None
                    assert item.name == 'TestItem'
        

This test ensures that an item can be created and stored in the database.

Step 4: Mocking Database Interactions

Mocking helps you test application logic without relying on a real database. Use the unittest.mock library to mock database queries:

            from unittest.mock import patch

            def test_mocked_get_items(client):
                with patch('app.Item.query') as mock_query:
                    mock_query.all.return_value = [Item(id=1, name='MockItem')]
                    response = client.get('/items')
                    assert response.status_code == 200
                    assert response.json == [{'id': 1, 'name': 'MockItem'}]
        

In this example, the database query is mocked to return predefined data.

Step 5: Running the Tests

Run the tests using pytest:

            pytest test_app.py
        

The output will show the results of the tests, indicating whether they passed or failed.

Conclusion

In this article, we demonstrated how to write unit tests for views, models, and forms in a Flask application. We also explored mocking database interactions to test application logic without relying on a real database. By following these steps, you can ensure your Flask application is reliable and bug-free.

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java