Wednesday, August 19, 2026
HomeArtificial IntelligenceA Mild Introduction to Unit Testing in Python

A Mild Introduction to Unit Testing in Python

[ad_1]

Final Up to date on March 15, 2022

Unit testing is a technique for testing software program which appears to be like on the smallest testable items of code, known 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 features which might not be uncovered to the consumer, works accurately and as supposed.

The concept 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 properly collectively and as supposed.

On this submit, you’ll uncover how you can implement unit testing in Python utilizing two widespread unit testing frameworks, the built-in PyUnit framework and the PyTest framework.

After finishing this tutorial, you’ll know:

  • Unit testing libraries in Python corresponding to PyUnit and PyTest
  • Checking anticipated operate conduct by way of the usage of unit exams

Let’s get began!

A Mild Introduction to Unit Testing in Python
Picture by Bee Naturalles. Some rights reserved.

Overview

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

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

What are unit exams and why are they essential?

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

Now, prolong that concept to code! We wouldn’t wish to need to always 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 truly returns the world 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 concept of a unit check! A unit check is a check which checks a single element of code, normally modularized as a operate, and guaranteeing that it performs as anticipated.

Unit exams are an essential a part of regression testing to make sure that the code nonetheless features as anticipated after now we have made modifications to the code and helps to make sure code stability. After making modifications to our code, we will run the unit exams now we have created beforehand to make sure that the present performance in different elements of the codebase has not been impacted by our modifications.

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

That’s the place unit exams are available in, we will 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 start line earlier than we begin wanting on the integration of parts in integration testing.

For the remainder of the article, we might be exhibiting how you can do unit testing by testing the features on this Rectangle class

Now that now we have motivated unit exams, let’s discover how precisely we will use unit exams as a part of our improvement pipeline and how you can implement them in Python!

Check Pushed Improvement

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

  • You aren’t allowed to write down any manufacturing code until it’s to make a failing unit check move.
  • You aren’t allowed to write down any extra of a unit check than is enough to fail; and compilation failures are failures.
  • You aren’t allowed to write down any extra manufacturing code than is enough to move the one failing unit check.

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

TDD additionally biases in direction of having smaller exams which suggests 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 in your initiatives. However chances are you’ll think about that as a technique to develop your code and the exams on the similar time.

Utilizing Python built-in PyUnit framework

You could be questioning, why do we want unit testing frameworks since Python and different languages provide 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 operate with completely different parameters, test for anticipated exceptions, and plenty 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 will get began writing out first unit check. Unit exams in PyUnit are structured as subclasses of the unittest.TestCase class and we will override the runTest() methodology to carry out our personal unit exams which test situations utilizing completely different assert features in unittest.TestCase:

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

Utilizing the completely different assert features in unittest.TestCase additionally provides us a greater capability to check completely different behaviors corresponding 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.essential() 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:

Word: Whereas within the above, our enterprise logic Rectangle class and our check code TestGetAreaRectangle are put collectively, in actuality chances are you’ll put them in separate information and import the enterprise logic into your check code. This can assist you higher handle the code.

We will 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 can give us our first error:

We will 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.

If we had some code that we wanted to run to arrange earlier than operating every check, we will override the setUp() methodology:

Within the above code instance, now we have overridden the setUp() methodology from unittest.TestCase, with our personal setUp() methodology that initializes a Rectangle object. This setUp() methodology 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 much like the @Earlier than decorator in JUnit.

Likewise, there’s a tearDown() methodology that we will override as properly for code to be executed after every check.

We will additionally combination check circumstances 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 solution to run exams in PyUnit which is utilizing the unittest.TextTestRunner class, which permits us to run particular check suites.

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

The whole code is as follows:

That is simply the tip of the iceberg with what you are able to do with PyUnit. We will 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 write down features with names prefixed with “check” and PyTest’s check discovery process will have the ability to discover your exams, e.g.,

You’ll discover that PyTest makes use of Python’s built-in assert key phrase as an alternative of its personal set of assert features as PyUnit does, which could make it barely extra handy since we will keep away from looking out up the completely different assert features.

The whole code is as follows:

After saving this right into a file test_file.py, we will 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.essential(). However in PyTest, we merely move the file to the module. The PyTest module will acquire all of the features outlined with prefix check and name them one after the other. After which it would 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 features 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 an especially versatile fixture system, the place fixtures are features which have a return worth. PyTest’s fixture system permits for sharing of fixtures throughout courses, modules, packages, or periods, 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 listing of test_negative_case with the fixture and gives test_negative_case with its personal set of outputs from the rectangle operate, and does this for each different check. Nonetheless, observe that fixtures may be requested greater than as soon as per check and for every check, the fixture is barely run as soon as and the result’s cached. Which means all references to that fixture throughout the operating of a person check are referencing the identical return worth (which is essential if the return worth is a reference kind).

The whole code is as follows:

Like PyUnit, PyTest has lots of different performance that may help 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 operate that will get inventory information from Yahoo Finance utilizing pandas_datareader and doing this in PyUnit:

This operate will get the inventory information on a specific 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 info reader might didn’t return something (if Yahoo Finance is down), or return a DataFrame with lacking columns or lacking information within the columns (if the supply restructured its web site). Due to this fact, we should always present a number of check features to test for a number of modes of failure:

Our collection 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 information within the DataFrame was throughout the final 7 days (test_most_recent_within_week).

Think about you’re doing a machine studying undertaking that consumes the inventory market information. Having a unit check framework can assist you establish in case your information preprocessing is working as anticipated.

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

Should you prefer it then you must have put a check on it

— Software program Engineering at Google

Additional studying

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

Libraries

Articles

Books

Abstract

On this submit, you found what unit testing is and how you can use two widespread libraries in Python to conduct unit testing (PyUnit, PyTest). You have got additionally learnt how you can configure unit exams and seen an instance of a use case for unit testing within the information science pipeline.

Particularly, you discovered:

  • what unit testing is and why it’s helpful
  • how unit testing matches throughout the Check Pushed Improvement pipeline
  • how you can 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