{"meta":{"title":"Updating unit tests to match code changes","intro":"Copilot Chat can help with updating your tests.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/copilot","title":"GitHub Copilot"},{"href":"/en/copilot/tutorials","title":"Tutorials"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat Cookbook"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/testing-code","title":"Testing code"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/testing-code/update-unit-tests","title":"Update unit tests"}],"documentType":"article"},"body":"# Updating unit tests to match code changes\n\nCopilot Chat can help with updating your tests.\n\nWhen you make changes to your code, it's important to update any tests to verify the new behavior and catch any bugs that the new code has introduced. Copilot Chat can help you quickly update tests to match your code changes, ensuring your test suite stays in sync with your implementation.\n\n## Example scenario\n\nImagine you have a Python function, `calculate_discount`, that determines the discount for a given purchase amount. In the original code, you get a 10% discount for amounts over $100. You're making changes to the logic of the function, so that only prices over $150 get a 10% discount, and there's now a 20% discount for amounts over $200.\n\n### Original code\n\nIn the original code, purchase prices above $100 get a 10% discount.\n\n```python\ndef calculate_discount(amount: float) -> float:\n    if amount > 100:\n        return amount * 0.1  # 10% discount\n    return 0.0\n```\n\n### Updated code\n\nIn the changed code, only amounts above $150 get 10% discount, and amounts above $200 now get a 20% discount.\n\n```python id=calculate_discount\ndef calculate_discount(amount: float) -> float:\n    if amount > 200:\n        return amount * 0.2  \n    elif amount > 150:\n        return amount * 0.1\n    return 0.0\n```\n\n### Existing tests\n\nThis example shows the existing tests that were written for the original code.\n\n```python\nimport unittest\nfrom discount import calculate_discount\n\nclass TestCalculateDiscount(unittest.TestCase):\n    def test_discount_above_100(self):\n        # $140 should get a $14 discount (10%)\n        self.assertAlmostEqual(calculate_discount(140), 14.0)\n    \n    def test_discount_below_100(self):\n        # $80 should get no discount\n        self.assertEqual(calculate_discount(80), 0.0)\n\n    def test_discount_exactly_100(self):\n        # $100 is not above 100, so no discount\n        self.assertEqual(calculate_discount(100), 0.0)\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\n## Example prompt\n\nYou can ask Copilot Chat to identify and update any tests affected by your logic change.\n\n```copilot\nGiven the update to the `calculate_discount` function, update the unit tests that may fail or become outdated as a result.\n```\n\n## Example response\n\n> \\[!NOTE] The following response is an example. Copilot Chat responses are non-deterministic, so you may get a different response from the one shown here.\n\nCopilot Chat analyzes your code and existing tests, and identifies which tests will fail or are now misleading following your code change.\n\nFor example, Copilot Chat explains that:\n\n* `test_discount_above_100` will now **fail**\n* `test_discount_below_100` and `test_discount_exactly_100` will pass, but they are now **misleading**, since the threshold for the 10% discount is now $150, and not $100.\n\nIn addition, Copilot Chat identifies any **missing test coverage**, such as the new 20% discount tier for amounts over $200.\n\nCopilot Chat proposes an updated set of tests.\n\n### Updated tests\n\n```python\nimport unittest\nfrom discount import calculate_discount\n\nclass TestCalculateDiscount(unittest.TestCase):\n    def test_discount_above_200(self):\n        # $250 should get a $50 discount (20%)\n        self.assertAlmostEqual(calculate_discount(250), 50.0)\n\n    def test_discount_above_150(self):\n        # $175 should get a $17.50 discount (10%)\n        self.assertAlmostEqual(calculate_discount(175), 17.5)\n\n    def test_discount_at_or_below_150(self):\n        # $150 is not above 150, so no discount\n        self.assertEqual(calculate_discount(150), 0.0)\n        # $80 should get no discount\n        self.assertEqual(calculate_discount(80), 0.0)\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\n## Further reading\n\n* [Prompt engineering for GitHub Copilot Chat](/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [Best practices for using GitHub Copilot](/en/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}