Introduction to testing in Python

Introduction to Testing in Python

Alexander Levin

Data Scientist

Why is testing so important?

Common problems:

  • Bugs and errors
  • Hardware failures
  • Unexpected or unpredictable behavior

All of the problems might lead to significant expenses increase for the fixing purposes.

Testing helps to:

  • Identify defects, bugs, and errors
  • Reduce risks of software failures
  • Enhance reliability, functionality, and performance of the software
Introduction to Testing in Python

What is testing?

  • Testing - a process of evaluating a system or software
  • We need testing to ensure it meets specified requirements

  • Test - a procedure to verify the correctness of a software application or system

Introduction to Testing in Python

Course prerequisites

  • Advanced Python programming
  • assert statements
  • Decorators
  • OOP Concepts (classes, methods, inheritance)
Introduction to Testing in Python

Testing in real life

Think of airplanes:

  • Visual inspection
  • Electronics and mechanics check
  • Fuel check
  • Passengers check
  • Weather check
  • Permission to take off from air traffic controller

All of the above - are tests! And we need them for safety.

airplane before take-off

Introduction to Testing in Python

Assert in Python

  • assert condition - lets to test if condition is True.
  • If condition is False, Python will raise an AssertionError.
Introduction to Testing in Python

Testing with pytest - a simple example

pytest - a popular testing framework in Python, which provides a simple way to write tests.

Example of an "assert" test written with pytest in Python:

import pytest

# A function to test
def squared(number):
    return number * number

# A test function always starts with "test"
def test_squared():
    assert squared(-2) == squared(2)
Introduction to Testing in Python

Context managers recap

  • Context manager - a Python object that is used by declaring a with statement
  • We use context managers to set up and tear down temporary context
# Writing to file example
with open("hello_world.txt", 'w') as hello_file:
    hello_file.write("Hello world \n")
Introduction to Testing in Python

Meet the pytest: raises

pytest.raises - when you expect the test to raise an Exception

import pytest

# A function to test
def division(a, b):
    return a / b

# A test function
def test_raises():
    with pytest.raises(ZeroDivisionError):
        division(a=25, b=0)
Introduction to Testing in Python

Summary

Testing is:

  • A process of evaluating, that software works as expected
  • Present in everyday life
  • Essential to tackle the challenges of the software development process
  • Helps to ensure that the problems are addressed properly

Tests implementation:

  • pytest - a powerful Python framework, that simplifies the testing process
  • assert - a Python keyword, used in pytest for creating basic tests by validating a condition
  • pytest.raises- a context manager, used to create a test that is expected to result in Exception
Introduction to Testing in Python

Let's practice!

Introduction to Testing in Python

Preparing Video For Download...