|
| 1 | +--- |
| 2 | +title: Updating unit tests to match code changes |
| 3 | +shortTitle: Update unit tests |
| 4 | +intro: '{% data variables.copilot.copilot_chat_short %} can help with updating your tests.' |
| 5 | +versions: |
| 6 | + feature: copilot |
| 7 | +category: |
| 8 | + - Testing code |
| 9 | + - Author and optimize with Copilot |
| 10 | +complexity: |
| 11 | + - Intermediate |
| 12 | +octicon: beaker |
| 13 | +topics: |
| 14 | + - Copilot |
| 15 | +contentType: tutorials |
| 16 | +--- |
| 17 | + |
| 18 | +When 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. {% data variables.copilot.copilot_chat_short %} can help you quickly update tests to match your code changes, ensuring your test suite stays in sync with your implementation. |
| 19 | + |
| 20 | +## Example scenario |
| 21 | + |
| 22 | +Imagine 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. |
| 23 | + |
| 24 | +### Original code |
| 25 | + |
| 26 | +In the original code, purchase prices above $100 get a 10% discount. |
| 27 | + |
| 28 | +```python |
| 29 | +def calculate_discount(amount: float) -> float: |
| 30 | + if amount > 100: |
| 31 | + return amount * 0.1 # 10% discount |
| 32 | + return 0.0 |
| 33 | +``` |
| 34 | + |
| 35 | +### Updated code |
| 36 | + |
| 37 | +In the changed code, only amounts above $150 get 10% discount, and amounts above $200 now get a 20% discount. |
| 38 | + |
| 39 | +```python id=calculate_discount |
| 40 | +def calculate_discount(amount: float) -> float: |
| 41 | + if amount > 200: |
| 42 | + return amount * 0.2 |
| 43 | + elif amount > 150: |
| 44 | + return amount * 0.1 |
| 45 | + return 0.0 |
| 46 | +``` |
| 47 | + |
| 48 | +### Existing tests |
| 49 | + |
| 50 | +This example shows the existing tests that were written for the original code. |
| 51 | + |
| 52 | +```python |
| 53 | +import unittest |
| 54 | +from discount import calculate_discount |
| 55 | + |
| 56 | +class TestCalculateDiscount(unittest.TestCase): |
| 57 | + def test_discount_above_100(self): |
| 58 | + # $140 should get a $14 discount (10%) |
| 59 | + self.assertAlmostEqual(calculate_discount(140), 14.0) |
| 60 | + |
| 61 | + def test_discount_below_100(self): |
| 62 | + # $80 should get no discount |
| 63 | + self.assertEqual(calculate_discount(80), 0.0) |
| 64 | + |
| 65 | + def test_discount_exactly_100(self): |
| 66 | + # $100 is not above 100, so no discount |
| 67 | + self.assertEqual(calculate_discount(100), 0.0) |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + unittest.main() |
| 71 | +``` |
| 72 | + |
| 73 | +## Example prompt |
| 74 | + |
| 75 | +You can ask {% data variables.copilot.copilot_chat_short %} to identify and update any tests affected by your logic change. |
| 76 | + |
| 77 | +```copilot |
| 78 | +Given the update to the `calculate_discount` function, update the unit tests that may fail or become outdated as a result. |
| 79 | +``` |
| 80 | + |
| 81 | +## Example response |
| 82 | + |
| 83 | +{% data reusables.copilot.example-prompts.response-is-an-example %} |
| 84 | + |
| 85 | +{% data variables.copilot.copilot_chat_short %} analyzes your code and existing tests, and identifies which tests will fail or are now misleading following your code change. |
| 86 | + |
| 87 | +For example, {% data variables.copilot.copilot_chat_short %} explains that: |
| 88 | +* `test_discount_above_100` will now **fail** |
| 89 | +* `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. |
| 90 | + |
| 91 | +In addition, {% data variables.copilot.copilot_chat_short %} identifies any **missing test coverage**, such as the new 20% discount tier for amounts over $200. |
| 92 | + |
| 93 | +{% data variables.copilot.copilot_chat_short %} proposes an updated set of tests. |
| 94 | + |
| 95 | +### Updated tests |
| 96 | + |
| 97 | +```python |
| 98 | +import unittest |
| 99 | +from discount import calculate_discount |
| 100 | + |
| 101 | +class TestCalculateDiscount(unittest.TestCase): |
| 102 | + def test_discount_above_200(self): |
| 103 | + # $250 should get a $50 discount (20%) |
| 104 | + self.assertAlmostEqual(calculate_discount(250), 50.0) |
| 105 | + |
| 106 | + def test_discount_above_150(self): |
| 107 | + # $175 should get a $17.50 discount (10%) |
| 108 | + self.assertAlmostEqual(calculate_discount(175), 17.5) |
| 109 | + |
| 110 | + def test_discount_at_or_below_150(self): |
| 111 | + # $150 is not above 150, so no discount |
| 112 | + self.assertEqual(calculate_discount(150), 0.0) |
| 113 | + # $80 should get no discount |
| 114 | + self.assertEqual(calculate_discount(80), 0.0) |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + unittest.main() |
| 118 | +``` |
| 119 | + |
| 120 | +## Further reading |
| 121 | + |
| 122 | +{% data reusables.copilot.example-prompts.further-reading-items %} |
0 commit comments