Sam's Surf Shop

stuck at task 6. Please help.

import unittest
import surfshop

class SystemTest(unittest.TestCase):
  def setUp(self):
    self.cart = surfshop.ShoppingCart()
  def test_add1_surfboards(self):
    message = self.cart.add_surfboards()
    self.assertEqual(message, 'Successfully added 1 surfboard to cart!')
  def test_add2_surfboards(self):
    message = self.cart.add_surfboards(2)
    self.assertEqual(message, 'Successfully added 2 surfboards to cart!')
  def test_add_surfboards(self):
    self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards(5))

unittest.main()

the output seems to be alright when i have integer 5 as input
…E

ERROR: test_add_surfboards (main.SystemTest)

Traceback (most recent call last):
File “tests.py”, line 15, in test_add_surfboards
self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards(5))
File “/home/ccuser/workspace/sams-surf-shop/surfshop.py”, line 17, in add_surfboards
raise TooManyBoardsError
surfshop.TooManyBoardsError


Ran 3 tests in 0.000s

FAILED (errors=1)

But when I have 3 or 2 as input, the output seems odd

def test_add2_surfboards(self):
message = self.cart.add_surfboards(2)
self.assertEqual(message, ‘Successfully added 2 surfboards to cart!’)
def test_add_surfboards(self):
self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards(3))

unittest.main()
Output-only Terminal
Output:
…E

ERROR: test_add_surfboards (main.SystemTest)

Traceback (most recent call last):
File “tests.py”, line 15, in test_add_surfboards
self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards(3))
File “/usr/lib/python3.6/unittest/case.py”, line 733, in assertRaises
return context.handle(‘assertRaises’, args, kwargs)
File “/usr/lib/python3.6/unittest/case.py”, line 178, in handle
callable_obj(*args, **kwargs)
TypeError: ‘str’ object is not callable


Ran 3 tests in 0.001s

FAILED (errors=1)

1 Like

the raise of the Exception “TooManyBoardsError” pops because the setUp function runs only once when you call the subTest. What I did is to create an new instance for each of the subTest:

def test_add_more_surfboards(self):
    num_surfboards = [2, 3, 4]

    for num in num_surfboards:
      self.cart = surfshop.ShoppingCart()
      with self.subTest(num):
        self.assertEqual(self.cart.add_surfboards(num), f'Successfully added {num} surfboards to cart!', "Check adding surfboards to a cart")

if not, you will cumulate the surfboards in the instance created at the beginning of the tests.

1 Like

implementation of the test for checkout_date function:

import surfshop, unittest
from datetime import date


today = date.today()
yesterday = today.replace(day= (today.day)-1)

class surfShop_test(unittest.TestCase):
  def setUp(self):
    self.cart = surfshop.ShoppingCart()

  def test_dateValidation(self):
    self.assertRaises(surfshop.CheckoutDateError, self.cart.set_checkout_date, yesterday) 
  
    

In surfshop.py:

    def set_checkout_date(self, date):
        if date <= datetime.date.today():
            raise CheckoutDateError
        else:
            self.checkout_date = date

Put the argument for the ‘self.cart.add_surfboards’ function as the last argument for ‘self.assertRaises()’.
like this:

def test_add_surfboards(self):
    self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards, 5)