Python Testing Tutorial for Beginners #1

Why test?

It may sound boring, but tests keep the world in check. They help us diagnose issues, understand things and prevent failure.

Tests are also not just a ‘software’ thing. They are used in so many industries across the world:

  • Chemistry: A “Litmus test” tests materials for their pH level

  • Medicine: A “blood test” tests for all kinds of information

  • Electrician: A “socket test” tests that an electrical socket is safe to use

  • Banking: A “stress test” tests that a bank can withstand an economic shock

Have a think about your current job or field of study, what tests can you think of? Let me know in the comments!

When you’re writing code that you want to be confident that it is doing what you think it is. This is why we test.

Testing tutorial

1. Write your function

Before I walkthrough the basics of testing in Python, I will setup the function that we want to test. For the purposes of the tutorial, it is kept very simple, but the steps can be replicated to test much more complex functions as you grow as a Pythonista!

def cube(a):
    """
    Returns the result of: a * a * a 
    i.e. the cube of value a (a^3)
    """
        
    return a * a * a

The code above represents our python file some_maths.py. We’ve defined a function cube() that takes a number, and returns its cube value.

For example: 2 cubed is 8; 3 cubed is 27.

2. Install pytest

Go ahead and install pytest in your preferred environment.

This blog post assumes you are familiar with installing Python packages (if not, here are some links: conda install, pip install).

3. Create a test file

Once we have written our function, we can now test it. We will make use of a common Python library called pytest. It is one of the most commonly used Python libraries for testing code, and I highly recommend it.

A crucial thing to know when writing tests for your python code, is that the test file, must be prefixed with test_. This is linked to how pytest ‘discovers’ where your tests are in your directory. As you can see in my example pictured above, I have my function contained in somemaths.py and then my test is in a file called test_some_maths.py.

4. Write your test

When writing a test, I like to have the following steps in my head:

  1. Define the inputs.

  2. Define what you expect to happen.

  3. Run the function.

  4. Verify that the result, is what you expected

from some_maths import cube


def test_cube_fn_is_correct():
    """
    Test that the `cube()` returns the correct result!
    """
    
    # Define the inputs
    input_value = 3
    
    # Define what you expect to happen
    expected_result = 27
    
    # Run the function
    result = cube(input_value)
    
    # Verify that the result, is what you expected
    assert result == expected_result

I will now go through the above code in detail.

Firstly, we import the code we want to test. So we run: from some_maths import cube.

We then proceed to write out test for the cube() function. Note again that the test follows the pattern of being prefixed with test_. This is for the same reason we named our test file, it is linked to how pytest discovers the tests in our directories.

We set the input_value to 3, and armed with our knowledge of maths, we know the result of 3 cubed should be 27.

We then run this function and capture the result.

Finally, we use the builtin Python keyword assert to verify the result is what we expected. If the result is 27, as we expected, we are asserting 27 equals 27, and therefore our assertion becomes True.

5. Run your test (and hopefully, they pass!)

Once the test is written, we can now use it to see if our code works.

To do this, open up your command line (console/terminal/shell) and navigate to your project directory. Then you can type in: pytest test_some_maths.py

If you’ve followed along, you should see something like the below. It begins the test (“test session starts”), it finds 1 test (“collected 1 item”), and then tells you the results (“1 passed in 0.01s”).

Summary

In this post, you hopefully learnt:

  • Why testing is important

  • The steps to writing a test for your Python code

There is a lot more to learn about the world of testing, and hence why I’ve numbered this blog post as #1.

If you got some value out of this and would like to see more, please say hello in the comments and let me know what you learnt, any questions you have, and perhaps anything you’d like to understand in future blog posts/videos!

Previous
Previous

Take control of your notifications with Apple’s Scheduled Summary

Next
Next

Apple AirPods Pro Review: Best In Class