8.1 Frontend Testing
At this stage, we'll check out how to test and debug our multi-container app. Testing helps find and fix bugs and ensures all system components are working correctly.
To test the frontend on ReactJS, we’ll use Jest and React Testing Library.
Installing Jest and React Testing Library
Step 1. Install dependencies:
cd frontend
npm install --save-dev jest @testing-library/react @testing-library/jest-dom
Step 2. Configure Jest:
Create a file jest.config.js
in the frontend directory:
module.exports = {
setupFilesAfterEnv: ['
/src/setupTests.js'], testEnvironment: 'jsdom', };
Step 3. Create a file setupTests.js
:
Create a file setupTests.js
in the frontend/src directory:
import '@testing-library/jest-dom/extend-expect';
Step 4. Write tests for components
Create a test file for the TaskList
component in the frontend/src
directory:
import React from 'react';
import { render, screen } from '@testing-library/react';
import TaskList from './TaskList';
test('renders Task List heading', () => {
render(
);
const headingElement = screen.getByText(/Task List/i);
expect(headingElement).toBeInTheDocument();
});
Step 5. Run tests
To run the tests, use the command:
npm test
8.2 Backend Testing
For backend testing with Flask, we’re gonna use the unittest library.
Installing Dependencies:
cd ../backend
pip install Flask-Testing
Writing Tests for Endpoints
Create a tests
directory and a test_routes.py
file inside the backend directory:
import unittest
from app import app, db
from app.models import User, Task
class BasicTests(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
self.app = app.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_user_registration(self):
response = self.app.post('/register', json={
'username': 'testuser',
'password': 'testpassword'
})
self.assertEqual(response.status_code, 201)
def test_task_creation(self):
self.app.post('/register', json={
'username': 'testuser',
'password': 'testpassword'
})
access_token = self.app.post('/login', json={
'username': 'testuser',
'password': 'testpassword'
}).json['access_token']
response = self.app.post('/tasks', json={
'title': 'Test Task',
'description': 'Test Description',
'owner_id': 1,
'status': 'not completed'
}, headers={'Authorization': f'Bearer {access_token}'})
self.assertEqual(response.status_code, 201)
if __name__ == "__main__":
unittest.main()
Running Tests
To run the tests, use the command:
python -m unittest discover
8.3 Integration Testing
Integration testing checks the interaction between system components.
Setting up the test environment
For integration testing, we will use Docker Compose to bring up all the services in the test environment.
Step 1. Create a Docker Compose file for tests:
Create a file docker-compose.test.yml
in the root directory of the project:
version: '3'
services:
frontend:
build: ./frontend
ports:
- "3001:3000"
networks:
- task-network
backend:
build: ./backend
ports:
- "5001:5000"
depends_on:
- database
networks:
- task-network
environment:
- DATABASE_URL=postgresql://taskuser:taskpassword@database:5432/taskdb
database:
image: postgres:13
environment:
- POSTGRES_DB=taskdb
- POSTGRES_USER=taskuser
- POSTGRES_PASSWORD=taskpassword
networks:
- task-network
volumes:
- db-data:/var/lib/postgresql/data
networks:
task-network:
driver: bridge
volumes:
db-data:
Step 2. Start the test environment:
To bring up all the services in the test environment, use the command:
docker compose -f docker-compose.test.yml up --build
Writing integration tests
Integration tests can be written using various tools. One approach is using Selenium for automated user interface testing.
Step 1. Install Selenium:
pip install selenium
Step 2. Write an integration test:
Create a file test_integration.py
in the tests
directory:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def test_frontend():
driver = webdriver.Chrome()
driver.get("http://localhost:3001")
assert "Task List" in driver.title
driver.close()
if __name__ == "__main__":
test_frontend()
Step 3. Run the integration test:
To run the integration test, use the command:
python tests/test_integration.py
8.4 Debugging
You can use container logs and monitoring tools for debugging.
Viewing container logs
To view logs, use the command:
docker compose logs
Using monitoring tools
Use the Prometheus and Grafana you configured earlier to monitor metrics and service statuses.
- Go to the Grafana web interface: Open a browser and navigate to
http://localhost:3033
. - Check out the dashboards: Use the dashboards you created to track service performance and statuses.
GO TO FULL VERSION