Back to Repositories

Testing Builder Pattern Building Construction in python-patterns

This test suite validates the Builder pattern implementation for creating different types of buildings (House, Flat, ComplexHouse) in Python. It verifies both simple and complex construction scenarios through dedicated test classes.

Test Coverage Overview

The test suite provides comprehensive coverage of building object construction scenarios.

Key areas tested include:
  • Simple house construction with size and floor verification
  • Flat construction with appropriate property validation
  • Complex house building using the construct_building director
Edge cases focus on proper property initialization and builder pattern implementation validation.

Implementation Analysis

The testing approach uses unittest’s assertion framework to verify object properties after construction. The implementation follows classic Builder pattern testing practices with separate test classes for simple and complex scenarios.

Technical patterns include:
  • Individual test methods for each building type
  • Property assertion verification
  • Director pattern testing with construct_building

Technical Details

Testing tools and configuration:
  • Python unittest framework
  • TestCase class inheritance
  • assertEqual assertion method
  • Modular test organization with TestSimple and TestComplex classes

Best Practices Demonstrated

The test suite demonstrates solid testing practices through clear separation of concerns and focused test methods.

Notable practices include:
  • Logical test class organization
  • Descriptive test method names
  • Focused assertions for specific properties
  • Clean separation between simple and complex building scenarios

faif/python-patterns

tests/creational/test_builder.py

            
import unittest

from patterns.creational.builder import ComplexHouse, Flat, House, construct_building


class TestSimple(unittest.TestCase):
    def test_house(self):
        house = House()
        self.assertEqual(house.size, "Big")
        self.assertEqual(house.floor, "One")

    def test_flat(self):
        flat = Flat()
        self.assertEqual(flat.size, "Small")
        self.assertEqual(flat.floor, "More than One")


class TestComplex(unittest.TestCase):
    def test_house(self):
        house = construct_building(ComplexHouse)
        self.assertEqual(house.size, "Big and fancy")
        self.assertEqual(house.floor, "One")