Saturday, November 7, 2026
HomeArtificial IntelligenceA Light Introduction to Unit Testing in Python

A Light Introduction to Unit Testing in Python

[ad_1]

Final Up to date on March 22, 2022

Unit testing is a technique for testing software program which appears on the smallest testable items of code, referred to as items, are examined for proper operation. By doing unit testing, we’re capable of confirm that every a part of the code, together with helper capabilities which might not be uncovered to the person, works accurately and as supposed.

The thought is that we’re independently checking every small piece of our program to make sure that it really works. This contrasts with regression and integration testing which exams that the completely different elements of this system work nicely collectively and as supposed.

On this publish, you’ll uncover easy methods to implement unit testing in Python utilizing two common unit testing frameworks, the built-in PyUnit framework and the PyTest framework.

After finishing this tutorial, you’ll know:

  • Unit testing libraries in Python comparable to PyUnit and PyTest
  • Checking anticipated perform habits by way of the usage of unit exams

Let’s get began!

A Light Introduction to Unit Testing in Python
Photograph by Bee Naturalles. Some rights reserved.

Overview

The tutorial is split into 5 elements, they’re:

  • What are unit exams and why are they necessary?
  • What’s Check Pushed Growth (TDD)?
  • Utilizing Python’s built-in PyUnit framework
  • Utilizing PyTest library
  • Unit testing in motion

What are unit exams and why are they necessary?

Bear in mind doing math again at school, finishing completely different arithmetic procedures earlier than combining them to get the proper reply? Think about how you’ll test to make sure that the calculations achieved at every step have been appropriate and also you didn’t make any careless errors or miswrote something.

Now, lengthen that concept to code! We wouldn’t need to should continuously look by way of our code to statically confirm its correctness, so how would you create a check to make sure that the next piece of code really returns the realm of the rectangle?

We may run the code with a number of check examples and see if it returns the anticipated output.

That’s the thought of a unit check! A unit check is a check which checks a single element of code, often modularized as a perform, and making certain that it performs as anticipated.

Unit exams are an necessary a part of regression testing to make sure that the code nonetheless capabilities as anticipated after we’ve got made adjustments to the code and helps to make sure code stability. After making adjustments to our code, we are able to run the unit exams we’ve got created beforehand to make sure that the prevailing performance in different elements of the codebase has not been impacted by our adjustments.

One other key advantage of unit exams is that they assist to simply isolate errors. Think about operating the complete challenge and receiving a string of errors. How would we go about debugging our code?

That’s the place unit exams are available in, we are able to analyze the outputs of our unit exams to see if any element of our code has been throwing errors and begin debugging from there. That’s to not say that unit testing can all the time assist us discover the bug, nevertheless it permits for a way more handy place to begin earlier than we begin wanting on the integration of parts in integration testing.

For the remainder of the article, we shall be exhibiting easy methods to do unit testing by testing the capabilities on this Rectangle class

Now that we’ve got motivated unit exams, let’s discover how precisely we are able to use unit exams as a part of our improvement pipeline and easy methods to implement them in Python!

Check Pushed Growth

Testing is so necessary to good software program improvement that there’s even a software program improvement course of primarily based on testing, which is Check Pushed Growth (TDD). Three guidelines of TDD proposed by Robert C. Martin are:

  • You aren’t allowed to jot down any manufacturing code except it’s to make a failing unit check cross.
  • You aren’t allowed to jot down any extra of a unit check than is ample to fail; and compilation failures are failures.
  • You aren’t allowed to jot down any extra manufacturing code than is ample to cross the one failing unit check.

The important thing thought of TDD is that we base our software program improvement round a set of unit exams that we’ve got created, which makes unit testing the center of the TDD software program improvement course of. On this method, you assured that you’ve got a check for each element you developed.

TDD additionally biases in direction of having smaller exams which implies exams which can be extra particular and check fewer parts at a time. This aids in monitoring down errors and smaller exams are additionally simpler to learn and perceive since there are much less parts at play in a single run.

It doesn’t imply you could use TDD to your tasks. However you could think about that as a technique to develop your code and the exams on the identical time.

Utilizing Python built-in PyUnit framework

You is likely to be questioning, why do we’d like unit testing frameworks since Python and different languages supply the assert key phrase? Unit testing frameworks assist to automate the testing course of and permit us to run a number of exams on the identical perform with completely different parameters, test for anticipated exceptions, and lots of others.

PyUnit is Python’s built-in unit testing framework and is Python’s model of the corresponding JUnit testing framework for Java. To get began constructing a check file, we have to import the unittest library to make use of PyUnit:

Then, we are able to get began writing out first unit check. Unit exams in PyUnit are structured as subclasses of the unittest.TestCase class and we are able to override the runTest() technique to carry out our personal unit exams which test situations utilizing completely different assert capabilities in unittest.TestCase:

That’s our first unit check! It checks if the rectangle.get_area() technique returns the proper space for a rectangle with width = 2 and size = 3. We use self.assertEqual as a substitute of merely utilizing assert to permit the unittest library to permit the runner to build up all check instances and produce a report.

Utilizing the completely different assert capabilities in unittest.TestCase additionally offers us a greater means to check completely different behaviors comparable to self.assertRaises(exception) which permits us to test if a sure block of code produces an anticipated exception.

To run the unit check, we make a name to unittest.primary() in our program,

Because the code returns the anticipated output for this case, it returns that the exams run efficiently, with the output:

The whole code is as follows:

Observe: Whereas within the above, our enterprise logic Rectangle class and our check code TestGetAreaRectangle are put collectively, in actuality you could put them in separate information and import the enterprise logic into your check code. This may help you higher handle the code.

We are able to additionally nest a number of unit exams collectively in a single subclass of unittest.TestCase, by naming strategies within the new subclass with the “check” prefix, for instance:

Working this may give us our first error:

We are able to see the unit check which failed, which is the test_negative_case as highlighted within the output together with the stderr message, since get_area() doesn’t return -1 as we anticipated in our check.

There are various completely different sorts of assert capabilities outlined within the unittest. TestCase class which we are able to use, for instance

We are able to even test whether or not a selected exception was thrown throughout execution

Now, we take a look at build up our exams. What if we had some code that we would have liked to run to arrange earlier than operating every check? Effectively, we are able to override the setUp technique in unittest.TestCase.

Within the above code instance, we’ve got overridden the setUp() technique from unittest.TestCase, with our personal setUp() technique that initializes a Rectangle object. This setUp() technique is run prior to every unit check and is useful in avoiding code duplication when a number of exams depend on the identical piece of code to arrange the check. That is just like the @Earlier than decorator in JUnit.

Likewise, there’s a tearDown() technique that we are able to override as nicely for code to be executed after every check.

To run the strategy solely as soon as per TestCase class, we are able to additionally use the setUpClass technique as follows.

The above code is just run as soon as per TestCase as a substitute of as soon as per check run as is the case with setUp.

To assist us set up exams and choose which set of exams we need to run, we are able to mixture check instances into check suites which assist to group exams that ought to executed collectively right into a single object:

Right here, we additionally introduce one other method to run exams in PyUnit which is utilizing the unittest.TextTestRunner class, which permits us to run particular check suites.

which supplies the identical output as operating the file from the command line and calling unittest.primary().

Bringing every little thing collectively, that is what the whole script for the unit check would appear to be:

That is simply the tip of the iceberg with what you are able to do with PyUnit. We are able to additionally write exams that search for exception messages that match a regex expression or setUp/tearDown strategies which can be run solely as soon as (setUpClass), for instance.

Utilizing PyTest

PyTest is an alternative choice to the built-in unittest module. To get began with PyTest, you’ll first want to put in it, which you are able to do utilizing

To put in writing exams, you simply want to jot down capabilities with names prefixed with “check” and PyTest’s check discovery process will be capable of discover your exams, e.g.,

You’ll discover that PyTest makes use of Python’s built-in assert key phrase as a substitute of its personal set of assert capabilities as PyUnit does, which could make it barely extra handy since we are able to keep away from looking up the completely different assert capabilities.

The whole code is as follows:

After saving this right into a file test_file.py, we are able to run PyTest unit check by:

And this provides us the output:

It’s possible you’ll discover that whereas in PyUnit, we have to invoke the check routine by a runner or calling unittest.primary(). However in PyTest, we merely cross the file to the module. The PyTest module will acquire all of the capabilities outlined with prefix check and name them one after the other. After which it’ll confirm if any exception is raised by the assert assertion. It may be extra handy to permit the exams stick with the enterprise logic.

PyTest additionally helps grouping capabilities collectively in courses, however the class ought to be named with prefix “Check” (with uppercase T), e.g.

Working this with PyTest will produce the next output:

The whole code is as follows:

To implement setup and teardown code for our exams, PyTest has a particularly versatile fixture system, the place fixtures are capabilities which have a return worth. PyTest’s fixture system permits for sharing of fixtures throughout courses, modules, packages, or classes, and fixtures that may name different fixtures as arguments.

Right here we embrace a easy introduction to PyTest’s fixture system,

The above code introduces rectangle as a fixture and PyTest matches the rectangle within the argument checklist of test_negative_case with the fixture and supplies test_negative_case with its personal set of outputs from the rectangle perform, and does this for each different check. Nonetheless, notice that fixtures will be requested greater than as soon as per check and for every check, the fixture is just run as soon as and the result’s cached. Because of this all references to that fixture through the operating of a person check are referencing the identical return worth (which is necessary if the return worth is a reference kind).

The whole code is as follows:

Like PyUnit, PyTest has numerous different performance that may will let you construct extra complete and superior unit exams.

Unit Testing in Motion

Now, we’ll discover unit testing in motion. For our instance, we’ll be testing a perform that will get inventory knowledge from Yahoo Finance utilizing pandas_datareader and doing this in PyUnit:

This perform will get the inventory knowledge on a selected inventory ticker by crawling from Yahoo Finance web site and returns the pandas DataFrame. This may fail in a number of methods, for instance, the information reader might did not return something (if Yahoo Finance is down), or return a DataFrame with lacking columns or lacking knowledge within the columns (if the supply restructured its web site). Due to this fact, we must always present a number of check capabilities to test for a number of modes of failure:

Our sequence of unit exams above test if sure columns are current (test_columns_present), whether or not the dataframe is non-empty (test_non_empty), whether or not the “excessive” and “low” columns are actually the excessive and low of the identical row (test_high_low), and whether or not the newest knowledge within the DataFrame was throughout the final 7 days (test_most_recent_within_week).

Think about you might be doing a machine studying challenge that consumes the inventory market knowledge. Having a unit check framework may help you determine in case your knowledge preprocessing is working as anticipated.

Utilizing these unit exams, we’re capable of determine if there was a fabric change within the output of our perform, and will be part of a Steady Integration (CI) course of. We are able to additionally connect different unit exams as required relying on the performance that we rely on from that perform.

For completeness, right here’s an equal model for PyTest.

Constructing unit exams might sound time consuming and tedious however they could be a crucial a part of any CI pipeline and are invaluable instruments for catching bugs early on earlier than they transfer additional down the pipeline and turn into extra pricey to deal with.

For those who prefer it then you need to have put a check on it

— Software program Engineering at Google

Additional studying

This part supplies extra assets on the subject if you’re seeking to go deeper.

Libraries

Articles

Books

Abstract

On this publish, you found what unit testing is and easy methods to use two common libraries in Python to conduct unit testing (PyUnit, PyTest). You might have additionally learnt easy methods to configure unit exams and seen an instance of a use case for unit testing within the knowledge science pipeline.

Particularly, you discovered:

  • what unit testing is and why it’s helpful
  • how unit testing suits throughout the Check Pushed Growth pipeline
  • easy methods to do unit testing in Python utilizing PyUnit and PyTest

 



[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments