Overview
The Python service uses pytest as the testing framework with SQLAlchemy database fixtures for test isolation. All tests run against a test database that is created fresh for each test function.Pytest Configuration
Test Fixtures (conftest.py)
Thetests/conftest.py file defines reusable fixtures:
Available Fixtures
app
app
Application instance configured for testing environment.Scope: Session (created once per test session)
db
db
Fresh database instance with all tables created.Scope: Function (recreated for each test)Cleanup: Automatically rolls back and drops all tables after each test
client
client
Flask test client for making HTTP requests.Usage:
client.get(), client.post(), etc.sample_user
sample_user
Pre-created user with credentials:
- Email:
[email protected] - Password:
testpassword123 - Name:
Test User
sample_products
sample_products
List of 5 products across different categories:
- Widget A ($29.99) - widgets category
- Widget B ($49.99) - widgets category
- Gadget X ($199.99) - gadgets category
- Gadget Y ($299.99) - gadgets category
- Tool Z ($15.99) - tools category
auth_token
auth_token
JWT access token for the sample user.Usage: Add to request headers:
sample_orders
sample_orders
5 pre-created orders with items for the sample user.
Test Files
test_auth.py - Authentication Tests
Tests for user registration and login endpoints. Example Test:- Successful registration with valid data
- Missing required fields validation
- Duplicate email prevention (409 conflict)
- Successful login with valid credentials
- Invalid password rejection (401 unauthorized)
- Non-existent user handling
test_products.py - Product Tests
Tests for product listing, search, and creation. Example Test:- Product listing with pagination
- Category filtering
- Search functionality
- Product retrieval by ID
- Product creation (requires authentication)
- SQL injection prevention in search
- 404 handling for non-existent products
test_orders.py - Order Tests
Tests for order creation and management. Example Test:- Order creation with multiple items
- Stock validation (insufficient stock handling)
- Discount code application
- Empty order rejection
- Order listing for authenticated users
- Single order retrieval
- Non-existent product handling
test_payments.py - Payment Tests
Tests for payment calculation and checkout processing. Example Test:- Tax calculation at 8.5% rate
- Percentage discount application (SAVE20 = 20% off)
- Flat discount application (FLAT5 = $5 off)
- Invalid discount code handling
- Payment total calculation
- Checkout flow completion
- Order status update to “paid”
test_security.py - Security Tests
Tests for SQL injection prevention and security vulnerabilities. Example Test:- Single quote handling in search
- UNION-based SQL injection prevention
- Boolean-based SQL injection prevention
- Input sanitization verification
Running Python Tests
Run all tests
-v: Verbose output with test names--tb=short: Shorter traceback format-k <pattern>: Run tests matching pattern-x: Stop after first failure
Test Output Example
Best Practices
The test database is automatically created and destroyed for each test function, ensuring complete isolation.