Friday, November 14, 2025

thumbnail

How to Perform End-to-End Testing in Full Stack Python Applications

 Tools You Can Use for End-to-End Testing in Full-Stack Python Applications


Backend Testing: For testing RESTful APIs in Python (e.g., Flask, Django, FastAPI):


pytest: A testing framework for Python that works well for unit and integration testing.


requests: A library for sending HTTP requests to test APIs.


Flask-Testing or Django Test: These libraries provide convenient ways to test Flask or Django apps.


Frontend Testing: For testing frontend functionality (HTML, JavaScript, and CSS):


Selenium: A browser automation tool that can simulate user interactions (clicks, form submissions, etc.).


Playwright: Another popular tool for browser automation (often more modern and easier to set up than Selenium).


Cypress: A front-end testing framework for integration testing that provides an interactive interface.


CI/CD Integration:


GitHub Actions, GitLab CI, CircleCI: These can run your tests automatically whenever code changes are pushed to the repository.


Step-by-Step Guide for End-to-End Testing


Let's break it down into different phases.


1. Backend Testing (API, Database, Server)

Setup for Backend Testing with Flask Example


Create a Flask API (or Django, FastAPI, etc.)


Make sure your application exposes some endpoints for frontend interaction.


Here's an example of a simple Flask app:


from flask import Flask, jsonify, request


app = Flask(__name__)


# Sample route for GET

@app.route('/api/greet', methods=['GET'])

def greet():

    return jsonify({"message": "Hello, World!"})


# Sample route for POST

@app.route('/api/user', methods=['POST'])

def create_user():

    data = request.get_json()

    return jsonify({"user_id": 1, "name": data['name']}), 201


if __name__ == "__main__":

    app.run(debug=True)



Write Tests for APIs (Using pytest and requests)


Example for testing GET and POST requests in Flask:


import pytest

from app import app  # Import your Flask app


@pytest.fixture

def client():

    with app.test_client() as client:

        yield client


def test_get_greet(client):

    response = client.get('/api/greet')

    assert response.status_code == 200

    assert response.json['message'] == "Hello, World!"


def test_post_create_user(client):

    data = {'name': 'John'}

    response = client.post('/api/user', json=data)

    assert response.status_code == 201

    assert response.json['user_id'] == 1

    assert response.json['name'] == 'John'



Run tests:


pytest



Test Database Interaction (if applicable):


Use an in-memory database (like SQLite) or mock the database for unit testing to avoid hitting the real database during tests.


In Flask, you can configure a test database using SQLAlchemy.


2. Frontend Testing (UI Testing with Selenium)


Selenium is commonly used for testing the UI of a web application. You can write tests to simulate user actions like clicking buttons, filling forms, and verifying elements.


Setup for Frontend Testing with Selenium


Install Selenium:


pip install selenium



Install WebDriver:

For Selenium to interact with the browser, you need to install a driver for the browser you are using (Chrome, Firefox, etc.). For Chrome, download ChromeDriver

.


Write Frontend Tests:


Here’s an example of using Selenium for E2E testing:


from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

import time


# Set up WebDriver (Chrome in this case)

driver = webdriver.Chrome(executable_path='/path/to/chromedriver')


def test_home_page():

    driver.get("http://localhost:5000")  # Assuming your Flask app is running locally

    assert "Welcome" in driver.title


def test_login():

    driver.get("http://localhost:5000/login")

    username = driver.find_element(By.NAME, "username")

    password = driver.find_element(By.NAME, "password")

    submit_button = driver.find_element(By.NAME, "submit")


    username.send_keys("testuser")

    password.send_keys("testpassword")

    submit_button.click()


    time.sleep(2)  # Wait for page to load

    assert "Dashboard" in driver.title


def teardown():

    driver.quit()


if __name__ == "__main__":

    test_home_page()

    test_login()

    teardown()



In this example, the tests simulate visiting the homepage and logging in.


3. End-to-End Testing (Frontend + Backend)

Setup for Full E2E Testing


To do E2E testing for both the frontend and backend, you typically run both the backend server and frontend (if they’re separate) and then simulate user interaction across both.


Run your application: Make sure the frontend and backend are running.


Backend: Run Flask or Django application on localhost:5000.


Frontend: Ensure your frontend (e.g., React, Vue) is running on another port (e.g., localhost:3000).


Test Interaction from Frontend to Backend:


If you're using tools like Cypress or Playwright, these tools can interact with the UI (as Selenium does) and make HTTP requests to the backend to ensure the full stack is working together.

Learn Fullstack Python Training in Hyderabad

Read More

Testing REST APIs in Python with Postman

Automating Tests for Full Stack Python Projects

How to Use PyTest for Full Stack Python Development

Writing Integration Tests for Python Web Applications

At Our Quality Thought Training Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive