student-fest

Beginner-friendly introduction to open source experience with coding challenges, real projects & student profiles #hacktoberfest

View on GitHub

🧪 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:

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

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:

  1. Install dependencies
  2. Run unit tests
  3. Check code coverage
  4. Run linting
  5. 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

Testing Checklist

Before submitting your PR:

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! 🧪✨