I'm writing unit tests for my webapp. Many of my test cases share the same boilerplate. For example, the tests for removing an item from the cart and updating the quantity of an item in the cart both start with navigating to the products page, searching for a product, and adding it to the cart.
Should such duplicated code be factored out of the unit tests somehow? Should I write a function add_item_to_cart
? But, I have another test test_add_to_cart
, which basically only consists of this duplicated boilerplate of adding to the cart.
Are unit tests by nature not DRY because of the need for each test to be independent?