Get startedGet started for free

Introduction to Testing in Python

1. Introduction to testing in Python

Hi, and welcome to the course! My name is Alexander, and I'll be your instructor. In this video, we will learn why testing is important, what testing is, and getting familiarize ourselves with the pytest framework. Let's start by discussing why we need testing at all.

2. Why is testing so important?

There are many challenges that occur during the software development process. Problems, such as bugs, errors, hardware failures, and unexpected software behavior might lead to significant expenses for fixing purposes. Testing helps to tackle those challenges.

3. What is testing?

But what is testing exactly? Testing is a process of evaluating a system to ensure it meets specified requirements. We use testing to discover errors by executing a special programs called "tests". A test is a procedure, that verifies the correctness and quality of a software application.

4. Course prerequisites

This course presumes a decent knowledge of Python programming. To fully benefit from it, we recommend understanding Python concepts like assert statements, decorators, and basic OOP concepts.

5. Testing in real life

Think of an airplane before a flight. How many tests are made before every single take-off? There are plenty, for example, visual inspection, electronics and mechanics check, fuel check, passengers check, weather check, and permission to take off from air traffic controller. In order to secure maximum safety, these procedures are documented, and it is essential to comply with all of them. But in fact, all of the above - are tests! And as we already discussed, we need them in software development for a very similar reason - safety.

6. Assert in Python

Assert is a Python keyword that lets you to test if a given condition returns True. Otherwise, you will get an "Assertion Error".

7. Testing with pytest - a simple example

Pytest is a popular Python testing framework. It provides a simple way to write tests. Let's discuss the example. Here, we have a function "squared", that takes a number and returns its square. The square of a number is a result of multiplying the number by itself. Let's assume that we decided to test this function. To do that, we created another function "test_squared" which contains a simple test inside. We assume the function works as expected if it returns the same value for minus two and two. Note that test function names always start with the word "test". That helps pytest to recognize test functions and distinguish them from regular ones. This is an example of how testing can be done. Of course, we could do more tests to test the "squared" function. While it is not exhaustive, it's a good start. In real life, we generally can't test for everything.

8. Context managers recap

Recall, that a context manager is a Python object that is used by declaring a "with" statement to set up and tear down temporary context. Assume you want to make sure that no division by zero will occur. To do that, we can use the context manager. Recall, that a context manager is a Python object that is used by declaring a "with" statement to set up and tear down temporary context.

9. Meet the pytest: raises

You can use pytest.raises context manager to ensure that the Exception is raised when it is expected to. Let's say we have a division function. We want it to forbid us passing a zero as a denominator. Otherwise, it should raise ZeroDivisionError. To implement this, we can use pytest.raises to check that ZeroDivisionError is raised if "b" equals zero.

10. Summary

Today, we started the journey into Python testing. Testing is quite common in everyday life. It is a process of evaluating that a system works as expected. We need testing to tackle the challenges of software development. We can use pytest framework, using such tools as "assert", and the "pytest.raises" context manager.

11. Let's practice!

You know the basics so far. Now try to create some tests of your own!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.