G2 - unit test

source: AWS

unit testing

  • testing the smallest functional unit of code to ensure code quality

G2 - unit test.png|500
image: AWS

unit test

  • a block of code that verifies the accuracy of a smaller, isolated block; typically a function or a method

strategies

examples

## method
def add_two_numbers(x, y):
    return x + y

## corresponding unit tests
def test_add_positives():
    result = add_two_numbers(5, 40)
    assert result == 45

def test_add_negatives():
    result = add_two_numbers(-4, -50)
    assert result == -54

def test_add_mixed():
    result = add_two_numbers(5, -5)
    assert result == 0  

best practices

other testing types