Testing and Quality Assurance with GitHub Copilot

Improve code reliability and testing efficiency using GitHub Copilot.

Tutorials dojo strip

Writing Unit and Integration Tests

GitHub Copilot can automatically suggest test cases when it detects a testing framework or a test function name. It analyzes your code and docstrings to propose logical tests.

  • Unit tests check individual functions or methods.
  • Integration tests verify that multiple modules or components work together correctly.

Example (Python with Pytest):

# main.py
def calculate_total(prices):
    return sum(prices)

# test_main.py
def test_calculate_total():
    # Copilot may suggest:
    assert calculate_total([10, 20, 30]) == 60
    assert calculate_total([]) == 0
    assert calculate_total([-10, 20]) == 10

When you start typing def test_calculate_total():, Copilot looks at your function calculate_total() and suggests likely tests. It includes normal use, empty input, and negative numbers.

You can type prompts like:

“Generate Jest test cases for the function calculateTotal in JavaScript.”

JavaScript Example:

// cart.js
function calculateTotal(prices) {
  return prices.reduce((a, b) => a + b, 0);
}

// cart.test.js
test('calculateTotal sums array values correctly', () => {
  expect(calculateTotal([10, 20, 30])).toBe(60);
  expect(calculateTotal([])).toBe(0);
  expect(calculateTotal([-5, 15])).toBe(10);
});

Identifying Edge Cases

Edge cases are rare or extreme inputs that can cause unexpected behavior. Copilot can help you think of these by suggesting unusual test values or error scenarios.

Copilot reviews your function and anticipates input types or boundary conditions that might break it.

Example:

def divide(a, b):
    return a / b

# test_divide.py
def test_divide():
    # Copilot may suggest:
    assert divide(10, 2) == 5
    assert divide(-10, 2) == -5
    # Edge case: division by zero
    try:
        divide(10, 0)
    except ZeroDivisionError:
        assert True

Copilot detects a possible “division by zero” error and proposes an exception-handling test. It can also test negative numbers or float inputs.

Prompt Copilot with:

“Suggest edge case tests for the divide function.”

Example:

function divide(a, b) {
  if (b === 0) throw new Error("Cannot divide by zero");
  return a / b;
}

test('divide handles edge cases', () => {
  expect(divide(10, 2)).toBe(5);
  expect(() => divide(10, 0)).toThrow("Cannot divide by zero");
  expect(divide(-10, 2)).toBe(-5);
});

Configuring Testing Settings in Copilot

You can fine-tune Copilot’s suggestions in your IDE to improve test accuracy.

In Visual Studio Code, for example, you can enable Copilot to offer inline suggestions in test files and adjust its completion behavior.

You can modify Copilot settings or use chat prompts to guide test generation, especially if you want Copilot to match your project’s framework or style.

Example:

In VS Code Settings (Ctrl + ,) > search for “Copilot” > enable:

  • Editor: Inline Suggest
  • Copilot: Enable for Test Files

Then, inside your test file, you can type:

“Write Pytest unit tests for the user authentication module.”

Copilot will generate something like:

def test_login_successful():
    user = authenticate_user("admin", "password123")
    assert user is not None

def test_login_invalid_password():
    user = authenticate_user("admin", "wrongpass")
    assert user is None

Configuring Copilot’s context allows it to make more accurate suggestions. If you’re testing APIs, you can also enable Copilot to read from your .env and config files (when allowed) for context-aware tests.

Reviewing Copilot’s Test Coverage Suggestions

After generating tests, you can review Copilot’s coverage by analyzing which functions are tested and which are missing.

Copilot might suggest additional test cases that improve your test coverage, ensuring that most of your code is validated.

Example:

“Suggest additional test cases to increase coverage for calculator.py.”

Copilot might generate tests for:

  • Negative inputs
  • Floating-point precision
  • Empty data
  • Invalid types

You can then use a test coverage tool (like pytest --cov or jest --coverage) to verify improvements.

Tutorials dojo strip
Tutorials dojo strip
Scroll to Top