Hello, I have a quick question on the usage of unittests, specifically, the subtest method.
in the Sam’s surf shop exercise project (https://www.codecademy.com/courses/learn-intermediate-python-3/projects/int-python-sams-surf-shop), step 10 tells us to parameterize a previous test.
# Write your code below:
import surfshop
import unittest
class SurfTest(unittest.TestCase):
def setUp(self):
self.cart = surfshop.ShoppingCart()
def test_add_surfboards(self):
for i in range(2,5):
with self.subTest(i=i):
test = self.cart.add_surfboards(i)
self.assertEqual(test, f'Successfully added {i} surfboards to cart!')
self.cart = surfshop.ShoppingCart()
def test_add_surfboards_duo(self):
test = self.cart.add_surfboards(2)
self.assertEqual(test, 'Successfully added 2 surfboards to cart!')
@unittest.skip
def test_TooManyBoardsError(self):
test = surfshop.TooManyBoardsError
self.assertRaises(test, self.cart.add_surfboards, 5)
@unittest.expectedFailure
def test_discount(self):
test = self.cart.apply_locals_discount()
test
self.assertTrue(self.cart.locals_discount)
unittest.main()
The last line on the test_add_surfboard function, it seems that if I don’t have it, the tests returns errors. Why is declaring:
self.cart = surfshop.ShoppingCart()
…a requirement for the test?