🧪 Testing Your Code - Interactive Student Fest
Welcome to our comprehensive testing guide! This document will help you ensure your contributions are robust, reliable, and ready for production.
Why Testing Matters
Testing is crucial for:
- Quality Assurance: Ensure your code works as expected
- Bug Prevention: Catch issues before they reach production
- Documentation: Tests serve as living documentation
- Confidence: Deploy with peace of mind
- Collaboration: Help other contributors understand your code
Testing Types
Unit Tests
Test individual functions, methods, or classes in isolation.
def test_add_numbers():
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0, 0) == 0
Integration Tests
Test how different parts of your application work together.
End-to-End Tests
Test the complete user experience from start to finish.
Performance Tests
Ensure your code performs well under load.
Setting Up Your Testing Environment
Python Testing Setup
pip install pytest pytest-cov
mkdir tests/
touch tests/__init__.py
JavaScript Testing Setup
npm install --save-dev jest supertest
npx jest --init
Java Testing Setup
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
Writing Tests
Test Naming Conventions
- Unit Tests:
test_function_name.py
- Test Methods:
test_should_do_something_when_condition
- Descriptive: Tests should explain what they’re testing
Test Structure (Arrange, Act, Assert)
def test_calculate_total_price():
# Arrange - Set up test data
items = [
{"name": "Widget", "price": 10.99, "quantity": 2},
{"name": "Gadget", "price": 5.49, "quantity": 1}
]
tax_rate = 0.08
# Act - Execute the code being tested
total = calculate_total(items, tax_rate)
# Assert - Verify the result
expected_total = (10.99 * 2 + 5.49) * (1 + tax_rate)
assert total == expected_total
Running Tests
Local Testing Commands
Python:
pytest
pytest tests/test_calculator.py
pytest --cov=myapp --cov-report=html
JavaScript:
npm test
npm run test:coverage
Java:
mvn test
Our Automated Testing
When you submit a PR, our GitHub Actions will automatically:
- Install dependencies
- Run unit tests
- Check code coverage
- Run linting
- Validate code style
Testing Best Practices
Test Isolation
# Good: Each test is independent
def test_deposit():
account = BankAccount()
account.deposit(100)
assert account.balance == 100
Descriptive Test Names
# Good
def test_should_return_empty_list_when_no_items_found():
def test_should_calculate_correct_total_with_tax_and_discount():
Test Data Management
@pytest.fixture
def sample_user():
return {
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"active": True
}
Language-Specific Testing
Python Testing
import pytest
@pytest.fixture
def temp_file(tmp_path):
file_path = tmp_path / "test.txt"
file_path.write_text("Hello, World!")
return file_path
JavaScript Testing
describe('User API', () => {
test('should create user successfully', async () => {
const userData = { name: 'John', email: 'john@test.com' };
// ... test code
});
});
Java Testing
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Test
void shouldReturnUserWhenFound() {
// Test implementation
}
}
Continuous Integration
GitHub Actions Testing Workflow
Our CI pipeline automatically runs when you submit a PR:
name: Test Suite
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, "3.10"]
steps:
- uses: actions/checkout@v4
- name: Set up Python $
uses: actions/setup-python@v4
with:
python-version: $
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pytest pytest-cov
- name: Run tests
run: pytest --cov=./ --cov-report=xml
Test Coverage Requirements
- Minimum Coverage: 80% for new code
- Critical Paths: 90%+ coverage for core functionality
- Integration Tests: Cover all API endpoints and user flows
Testing Checklist
Before submitting your PR:
- Unit Tests: All functions/methods have corresponding tests
- Edge Cases: Tests cover boundary conditions and error scenarios
- Integration Tests: Key user flows are tested end-to-end
- Code Coverage: Minimum 80% coverage achieved
- Local Testing: All tests pass locally
- CI Checks: Tests pass in GitHub Actions
Common Testing Issues & Solutions
Tests are slow
Solution: Use mocking, fixtures, and parallel test execution
Tests are brittle
Solution: Avoid testing implementation details, focus on behavior
Low test coverage
Solution: Use coverage tools to identify untested code paths
Tests don’t run in CI
Solution: Ensure all dependencies are properly specified
Additional Resources
Remember: Well-tested code is maintainable code. Happy testing! 🧪✨