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.

2 Likes

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)
1 Like

The solutions does not even include step 6!!! Can we have some guidance or explanation for the solutions??? A video explanation would be really appreciated! Or at least a step by step explanation!

8 Likes

Hey, thank you for confirming my expectations. I was trying to figure what was happening that I was getting an error on this checkpoint, and I had arrived to the same conclusion.

Agreed. A video would be great.

I am trying to edit the error that popped up when working on #8. Pl

[codebyte] [/codebyte] import surfshop import unittest class testing_function(unittest.TestCase): def setUp(self): self.surfshop.ShoppingCart = cart def test_add1_surfboards(self): message = self.cart.add.surfboards(1) 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_cart_limits(self): self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards, 5) @unittest.expectedFailure def test_local_discounts(self): self.local_discounts self.assertTrue(cart.apply_locals_discount, cart.locals_discount()) unittest.main()

ease assist!

I had the setup function written the other way:

  def setUp(self):
    self.cart = surfshop.ShoppingCart()
import surfshop, unittest class system_tests(unittest.TestCase): def setUp(self): self.cart = surfshop.ShoppingCart() def test_add1_surfboards(self): num_of_boards = self.cart.add.surfboards(1) self.assertEqual(num_of_boards, 'Successfully added 1 surfboard to cart!') def test_add2_surfboards(self): num_of_boards = self.cart.add.surfboards(2) self.assertEqual(num_of_boards, 'Successfully added 2 surfboards to cart!') def test_cart_limits(self): self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards, 5) @unittest.expectedFailure def test_local_discounts(self): self.local_discounts self.assertTrue(ShoppingCart.apply_locals_discount, ShoppingCart.locals_discount()) unittest.main()

I am having issues with the errors that are not supposed to show up in #8. Please advise. Thank you

figured it out! Thanks people!