Testing: Jest, pytest, Unit Tests, Integration Tests, TDD
320 words
2 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Testing: Jest, pytest, Unit Tests, Integration Tests, TDD ## 🎯 Learning Objectives - Write unit tests with pytest for Flask APIs - Write Jest tests for React components - Implement integration tests for full-stack features - Apply test-driven development (TDD) principles ## 📖 Core Content ### 1.1 pytest for Flas...

Testing: Jest, pytest, Unit Tests, Integration Tests, TDD
🎯 Learning Objectives
- Write unit tests with pytest for Flask APIs
- Write Jest tests for React components
- Implement integration tests for full-stack features
- Apply test-driven development (TDD) principles
📖 Core Content
1.1 pytest for Flask Backend
python# runnable # test_api.py import pytest from app import app @pytest.fixture def client(): app.config['TESTING'] = True with app.test_client() as client: yield client def test_login_success(client): """Test login with valid credentials.""" response = client.post('/auth/login', json={ 'email': '[email protected]', 'password': 'correct_password' }) assert response.status_code == 200 assert 'token' in response.json def test_login_invalid_email(client): """Test login with invalid email format.""" response = client.post('/auth/login', json={ 'email': 'not-an-email', 'password': 'password' }) assert response.status_code == 400
1.2 Jest for React
javascript// runnable // LoginForm.test.jsx import { render, screen, fireEvent } from '@testing-library/react'; import LoginForm from './LoginForm'; test('renders login form with submit button', () => { render(<LoginForm />); expect(screen.getByText('Login')).toBeInTheDocument(); expect(screen.getByLabelText('Email')).toBeInTheDocument(); }); test('shows error for invalid email', () => { render(<LoginForm />); fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'invalid' } }); fireEvent.click(screen.getByText('Login')); expect(screen.getByText('Invalid email format')).toBeInTheDocument(); });
1.3 Why This Matters
Testing prevents regressions and documents expected behavior. A project without tests becomes impossible to refactor. TDD (write tests first) ensures every feature has test coverage from the start.
2. 📝 Practice Questions
Q1: A bug report says "User registration fails silently." After fixing, write a test to prevent regression.pythondef test_registration_validation(client): """Registration with missing fields should return 400.""" # Missing last_name response = client.post('/auth/register', json={ 'first_name': 'John', 'email': '[email protected]', 'password': 'Password123' }) assert response.status_code == 400 assert 'last_name' in response.json['errors']This test ensures: (1) the API validates all required fields, (2) it returns a clear error message, (3) future changes don't accidentally remove this validation. Join Discord PreviousFile Uploads & Background TasksNextPerformance Optimization