Intermediate Python 4) Unit Tests

<< 3) Intermediate tutorial in Python – Threading

Testing your code is something that I think we all do to some extent.  You write a code snippet and throw in a bunch of print statements to make sure it’s working.  You’ve tested your code.  There comes a time where you start to reach a higher level in your programming maturity and learn the value in formal unit testing.  This article is meant to introduce you to unit testing in python and hopefully turn you into a believer if you dont already write tests for all of your code. 

There are several test platforms you can use to test your code, so I’m in no way trying to say that one is better than the other. Unittest is a great tool that comes packaged with python and naturally gets you writing tests quicker. pytest is also a good framework and allows you to write tests easier and is backwards compatible with python unittest tests. For the purposes of this article, I will be using pytest.

You may already have pytest installed, so check this first before trying to install it.

pytest --version
This is pytest version 5.0.0, imported from <your path to pytest>

Want To Work From Home? Take This Online Course To Find Out How

Installing Pytest

The easiest way to install pytest is using pip.

pip install -U pytest

Writing Your First Test

Consider the following code. The method square is the source code that we are testing and the method test_answer is the pytest test. Notice that it’s as easy as prefixing the method name with the keyword test.

def square(x):
    return x * x


def test_answer():
    assert square(3) == 9
    assert square(4) == 16
    assert square(5) == 25

The keyword assert basically means, “check to see if the next statement is true”. Notice that the statements should all evaluate to true(if the square method is written properly).

Running Your First test

This one is easy. Go to your working directly and run the following:

pytest

If your test passed, you should see something like this:

C:\Users\meraz\PycharmProjects\testproject>pytest
 ================================================= test session starts =================================================
 platform win32 -- Python 3.6.8, pytest-5.0.0, py-1.8.0, pluggy-0.12.0
 rootdir: C:\Users\meraz\PycharmProjects\testproject
 collected 1 item
 test_example.py .                                                                                                [100%]
 ============================================== 1 passed in 0.10 seconds ===============================================

Looking Forward

Code quality is ensured by a combination of good coding practices, good unit testing and thorough code review. The real power in writing good tests is that you leave the heavy lifting to the machines and catch things that humans may miss. You avoid unnecessary software regression issues and if you’re working on a team, it’s a good way to hold everyone accountable in making sure code continues to work as intended.

But what if your code interacts with other systems, such as cloud services or APIs, or some hardware integration? This is where integration tests come into play. End to end testing is necessary in ensuring that the interaction with these external systems is kept functional.

My Freelance Paycheck – Work from home doing what you love!

1 Comment

Leave a comment