Parameterization through an object incident - unit testing


I am doing the project for the unit testing part of intermediate python, I stumbled upon a problem that when reviewing the solutions I couldn’t understand why the line was added to somehow solve the problem.

When I run the code, it gives me an error message of the skipped test. I am assuming that the added line re-creates a new incident of the testing ‘environment’ I.e. the class; without it somehow iterating over and over caused the code to not skip the test which followed?

# Write your code below:
import unittest
import surfshop

class SurfShopCartTests(unittest.TestCase):

    def setUp(self):
        self.cart = surfshop.ShoppingCart()

  # tests:

    def test_add_surfboards(self):
      testing_parameters = [2,3,4]
      for board in testing_parameters:
        with self.subTest(board):
          result = self.cart.add_surfboards(board)
          expected = 'Successfully added {} surfboards to cart!'.format(board)
          self.assertEqual(result, expected)
          # self.cart = surfshop.ShoppingCart(). **** THIS IS THE LINE, IF I UNCOMMENT IT WORKS ****

    @unittest.skipIf(surfshop.off_season, "It's the off season")
    def test_error_add_surfboards(self):
      self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards, quantity=5)

    def test_applying_local_discounts(self):


Thank you so much, I am sorry I couldn’t explain it better <3

1 Like

When you look at the solution, does it show you the same line? And I believe you need to add that line because you used it prior at:

But it wasn’t defined, so without running the code or seeing your libraries, I imagine that’s the reason, also remember, when you make a function or whatever; not too sure with Python, but kotlin, for example, they are self-contained. You would have to call setup perhaps?


I think you’re asking the same question as here?

@grendeth9495 mentions the same issue, the setUp method will not be re-run for each subTest. So you’ve modified the underlying object and further tests will be complicated by this additional state (I think in this case your count would be higher than anticipated, right?).

You could make a lower cost test by just resetting the attributes you affect but be careful about anything that mutates the object as it can be very hard to keep track of through multiple tests (sometimes creating a new one is the sensible option).

1 Like

Yes, for Python as well, they are contained in that sense; I forgot about that as a student. Thank you so much <3 :smiley:

Ah I see, make sense; the test was to be made this way by the lesson :purple_heart: Thank you so much