From f7fb10d011c70a98254d4d87b85ec4849359b75f Mon Sep 17 00:00:00 2001 From: "Aleksander.Trinh" Date: Mon, 8 Nov 2021 11:55:34 +0100 Subject: [PATCH 1/2] added python file with copilot code and readme with instructions --- gallery/python-unittest-functions/README.md | 48 +++++++++++++++++++ .../python-unittest-functions/my_func_test.py | 0 2 files changed, 48 insertions(+) create mode 100644 gallery/python-unittest-functions/README.md create mode 100644 gallery/python-unittest-functions/my_func_test.py diff --git a/gallery/python-unittest-functions/README.md b/gallery/python-unittest-functions/README.md new file mode 100644 index 0000000..f80a123 --- /dev/null +++ b/gallery/python-unittest-functions/README.md @@ -0,0 +1,48 @@ +# [Copilot examples](../README.md) › [Unit testing with Copilot](README.md) (Python) + +In this example we write simple function using Copilot and then let it create mock data and unittests for us, thus giving complete control over our code to the Copilot. + + +## Setup: + +Requires Python (>3.8) and unittest library. + +## Steps: + +- Create a new file called `your_func_name.py` + +- Type `#function that delets number from array` on line 1 then on the next line start writing `def your_function_name` and press Tab to let Copilot complete your code. +- You can also use Ctrl + Enter to view other suggestions + +- Accept the following solution(example function `delete_numbers`): + + ```python + def delete_numbers(numbers, number): + for i in range(len(numbers)): + if numbers[i] == number: + del numbers[i] + return numbers + ``` + +- Type `#unit test for function delete_numbers`, go on to the next line and use Tab to allow Copilot complete code with the suggestions. +It should suggest mock data, like the following: + + ```python + test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + test_number = 5 + ``` + +- Go onto the next line and import `unittest`- this will hint Copilot you want to use `unittest` library for testing. On the next line start typing `class` and let Copilot autocomplete your code with proper unit test and assertions: + + ```python + import unittest + class TestDeleteNumbers(unittest.TestCase): + def test_delete_numbers(self): + self.assertEqual(delete_numbers(test_list, test_number), [1, 2, 3, 4, 6, 7, 8, 9, 10]) + ``` + +And you are done! You let copilot suggest and autocomplete code. It wrote a function you wanted and then you hinted it that you want to use `unittest` library for creating unittest. It provided you with data and even auto created a correct assertion. Amazing! Now creating simple functions and simple unit tests should be a breeze. You can see full file that was created by Github Copilot in the repo. + +# + +‹ [back to list of Copilot examples](../README.md) diff --git a/gallery/python-unittest-functions/my_func_test.py b/gallery/python-unittest-functions/my_func_test.py new file mode 100644 index 0000000..e69de29 From c68161b99e47b98a92b95d6b5fa2ebdac85bcf45 Mon Sep 17 00:00:00 2001 From: "Aleksander.Trinh" Date: Mon, 8 Nov 2021 11:57:32 +0100 Subject: [PATCH 2/2] added code to python file --- gallery/python-unittest-functions/my_func_test.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gallery/python-unittest-functions/my_func_test.py b/gallery/python-unittest-functions/my_func_test.py index e69de29..605b297 100644 --- a/gallery/python-unittest-functions/my_func_test.py +++ b/gallery/python-unittest-functions/my_func_test.py @@ -0,0 +1,15 @@ +#function that delets number from array +def delete_numbers(numbers, number): + for i in range(len(numbers)): + if numbers[i] == number: + del numbers[i] + return numbers + +#unit test for function delete_numbers +test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +test_number = 5 + +import unittest +class TestDeleteNumbers(unittest.TestCase): + def test_delete_numbers(self): + self.assertEqual(delete_numbers(test_list, test_number), [1, 2, 3, 4, 6, 7, 8, 9, 10]) \ No newline at end of file