Question

Functions test

Answers

GPT 3.5
To test the function, we can create different test cases and check if the function produces the expected output for each case. Here's an example of how we can test a simple function that adds two numbers:

```python
def add_numbers(a, b):
return a + b

def test_add_numbers():
# Test case 1: Positive numbers
assert add_numbers(1, 2) == 3

# Test case 2: Negative numbers
assert add_numbers(-1, -2) == -3

# Test case 3: One positive, one negative number
assert add_numbers(2, -3) == -1

# Test case 4: Zeroes
assert add_numbers(0, 0) == 0

test_add_numbers()
```

In this example, we define a function `add_numbers` that takes two arguments `a` and `b` and returns their sum. We also define a test function `test_add_numbers` that contains four test cases. Each test case asserts that the output of `add_numbers` matches the expected output.

To run the tests, we call `test_add_numbers()` at the end. If all the assertions pass, there will be no output. However, if any assertion fails, an `AssertionError` will be raised and the line number where it occurred will be displayed. This helps identify which test case failed and what the expected output was.

Related Questions