Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Latest commit

 

History

History
48 lines (32 loc) · 2.05 KB

File metadata and controls

48 lines (32 loc) · 2.05 KB

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):

      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:

      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:

      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