0
0
Testing Fundamentalstesting~8 mins

Unit testing in Testing Fundamentals - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Unit testing
Folder Structure for Unit Testing Framework
project-root/
├── src/
│   └── main_code/          # Your application source code
│       └── ...
├── tests/                  # Unit test files
│   ├── test_module1.py
│   ├── test_module2.py
│   └── ...
├── fixtures/               # Test data and setup helpers
│   └── ...
├── utils/                  # Utility functions for tests
│   └── ...
├── requirements.txt        # Dependencies
├── pytest.ini              # Test runner configuration
└── README.md
  
Test Framework Layers for Unit Testing
  • Test Cases Layer: Individual unit tests that check small pieces of code (functions, methods).
  • Test Utilities Layer: Helper functions to set up test data or common operations.
  • Fixtures Layer: Setup and teardown code to prepare environment or data before tests run.
  • Source Code Layer: The actual application code being tested.
  • Configuration Layer: Settings for test runner, environment variables, and dependencies.
Configuration Patterns for Unit Testing
  • Test Runner Config: Use pytest.ini or similar to define test discovery rules and options.
  • Environment Variables: Use environment variables or config files to switch between test and production settings.
  • Mocking External Calls: Replace external dependencies with mocks to isolate unit tests.
  • Test Data Management: Use fixtures or factory functions to create consistent test data.
Test Reporting and CI/CD Integration
  • Use test runners like pytest that provide clear pass/fail output and error details.
  • Generate reports in formats like JUnit XML for integration with CI tools (GitHub Actions, Jenkins).
  • Configure CI pipelines to run unit tests automatically on code push or pull requests.
  • Fail the build if any unit test fails to ensure code quality.
Best Practices for Unit Testing Frameworks
  • Isolate Tests: Each test should check one small piece of code without relying on others.
  • Use Clear Naming: Name test files and functions to describe what they test.
  • Keep Tests Fast: Unit tests should run quickly to encourage frequent runs.
  • Use Fixtures for Setup: Avoid repeating setup code by using fixtures or setup functions.
  • Mock External Dependencies: Replace calls to databases, APIs, or files with mocks to keep tests focused.
Self Check Question

Where in this folder structure would you add a new unit test for a function that calculates discounts?

Key Result
Unit testing frameworks organize small, fast, isolated tests with clear structure and configuration for reliable code validation.