Solution 1

class Cookie(object):
    DEFAULT_SCARF_COLOR = 'Green'
    DEFAULT_BUTTON_COLOR = 'Blue'

    def __init__(self, scarf_color, buttons_color):
        self.scarf_color = scarf_color
        self.buttons_color = buttons_color

    @classmethod
    def create_cookies(cls, amount, scarf_color=None, buttons_color=None):
        cookies = []
        for _ in range(amount):
            # cookies.append(Cookie())
            cookies.append(cls(
                scarf_color or cls.DEFAULT_SCARF_COLOR,
                buttons_color or cls.DEFAULT_BUTTON_COLOR))

        return cookies

Cookie Factory with default values

Extend the previous example create_cookies to receive two optional parameters (scarf_color and buttons_color). If they're not included, DEFAULT_SCARF_COLOR and DEFAULT_BUTTON_COLOR class attributes should be used. Example:

cookies = Cookie.create_cookies(5, scarf_color='Red', buttons_color='Yellow')
[<Cookie obj>, <Cookie obj>, <Cookie obj>, <Cookie obj>, <Cookie obj>]
# these cookies should have scarf_color == 'Red' and buttons_color == 'Yellow'

cookies = Cookie.create_cookies(3)
[<Cookie obj>, <Cookie obj>, <Cookie obj>]
# these cookies should have scarf_color == 'Green' and buttons_color == 'Blue'

Test Cases

test factory passing only scarf color - Run Test


def test_factory_passing_only_scarf_color():
    cookies = Cookie.create_cookies(3, scarf_color='red')
    assert len(cookies) == 3
    cookie_1 = cookies[0]
    cookie_2 = cookies[1]
    cookie_3 = cookies[2]

    # All with the same scarf color
    assert cookie_1.scarf_color == 'red'
    assert cookie_2.scarf_color == 'red'
    assert cookie_3.scarf_color == 'red'

    # All with the same buttons color
    assert cookie_1.buttons_color == 'Blue'
    assert cookie_2.buttons_color == 'Blue'
    assert cookie_3.buttons_color == 'Blue'

test factory passing both values - Run Test

def test_factory_passing_both_values():
    cookies = Cookie.create_cookies(
        5, scarf_color='red', buttons_color='yellow')
    assert len(cookies) == 5

    cookie_1 = cookies[0]
    cookie_2 = cookies[1]
    cookie_3 = cookies[2]
    cookie_4 = cookies[3]
    cookie_5 = cookies[4]

    # All with the same scarf color
    assert cookie_1.scarf_color == 'red'
    assert cookie_2.scarf_color == 'red'
    assert cookie_3.scarf_color == 'red'
    assert cookie_4.scarf_color == 'red'
    assert cookie_5.scarf_color == 'red'

    # All with the same buttons color
    assert cookie_1.buttons_color == 'yellow'
    assert cookie_2.buttons_color == 'yellow'
    assert cookie_3.buttons_color == 'yellow'
    assert cookie_4.buttons_color == 'yellow'
    assert cookie_5.buttons_color == 'yellow'

test factory passing no values - Run Test

def test_factory_passing_no_values():
    cookies = Cookie.create_cookies(4)  # No attributes passed
    assert len(cookies) == 4

    cookie_1 = cookies[0]
    cookie_2 = cookies[1]
    cookie_3 = cookies[2]
    cookie_4 = cookies[3]

    # All with the same scarf color
    assert cookie_1.scarf_color == 'Green'
    assert cookie_2.scarf_color == 'Green'
    assert cookie_3.scarf_color == 'Green'
    assert cookie_4.scarf_color == 'Green'

    # All with the same buttons color
    assert cookie_1.buttons_color == 'Blue'
    assert cookie_2.buttons_color == 'Blue'
    assert cookie_3.buttons_color == 'Blue'
    assert cookie_4.buttons_color == 'Blue'