1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/content/copilot/example-prompts-for-github-copilot-chat/testing-code/generate-unit-tests.md
Sarita Iyer 262ab79a12 [DO NOT MERGE] Official megabranch of "Copilot Chat Cookbook" with new map topic structure (#53046)
Co-authored-by: hubwriter <hubwriter@github.com>
Co-authored-by: Ben Ahmady <32935794+subatoi@users.noreply.github.com>
Co-authored-by: Tiago Pascoal <tspascoal@github.com>
Co-authored-by: Christopher Harrison <geektrainer@github.com>
Co-authored-by: Sophie <29382425+sophietheking@users.noreply.github.com>
Co-authored-by: Sunbrye Ly <56200261+sunbrye@users.noreply.github.com>
Co-authored-by: sunbrye <sunbrye@github.com>
Co-authored-by: Hector Alfaro <hectorsector@github.com>
Co-authored-by: Siara <108543037+SiaraMist@users.noreply.github.com>
2024-11-22 20:31:27 +00:00

80 lines
2.8 KiB
Markdown

---
title: 'Generate unit tests'
shortTitle: Generate unit tests
intro: '{% data variables.product.prodname_copilot_chat_short %} can help with generating unit tests for a function.'
versions:
feature: copilot
category:
- 'Testing code'
complexity:
- Intermediate
octicon: beaker
topics:
- Copilot
---
A good suite of unit tests is critical to the success of any project. However, writing these tests can be time-consuming and are often neglected. {% data variables.product.prodname_copilot_chat_short %} can help generate these tests for you.
## Example scenario
Imagine you have a Python function which validates data before updating the database. You need to create unit tests to ensure the validation performs the way you expect. You can ask {% data variables.product.prodname_copilot_chat_short %} to generate these tests for you.
```python
def validate_price(price: float):
if price <= 0:
raise ValueError("Price must be greater than 0")
if price > 1000:
raise ValueError("Price must be less than or equal to 1000")
```
## Example prompt
`/tests Generate unit tests for this function. Validate both success and failure, and include edge cases.`
## Example response
{% data reusables.copilot.example-prompts.response-is-an-example %}
If this is your first time asking {% data variables.product.prodname_copilot_chat_short %} to generate unit tests, it may ask you if you wish to configure unit tests for your project. For this example, we are assuming you decline this offer.
{% data variables.product.prodname_copilot_short %} will provide a strategy for generating the tests, and then provide the tests themselves.
```python
import unittest
from generate_unit_tests import validate_price
class TestValidateProduct(unittest.TestCase):
def test_price_zero(self):
with self.assertRaises(ValueError) as context:
validate_price(0)
self.assertEqual(str(context.exception), "Price must be greater than 0")
def test_price_negative(self):
with self.assertRaises(ValueError) as context:
validate_price(-10)
self.assertEqual(str(context.exception), "Price must be greater than 0")
def test_price_above_limit(self):
with self.assertRaises(ValueError) as context:
validate_price(1500)
self.assertEqual(str(context.exception), "Price must be less than or equal to 1000")
def test_price_edge_case_zero(self):
with self.assertRaises(ValueError) as context:
validate_price(0)
self.assertEqual(str(context.exception), "Price must be greater than 0")
def test_price_edge_case_max(self):
try:
validate_price(1000)
except ValueError:
self.fail("validate_price() raised ValueError unexpectedly!")
if __name__ == '__main__':
unittest.main()
```
## Further reading
{% data reusables.copilot.example-prompts.further-reading-items %}