Python unittest full walkthrough

Image for post

Photo by Jon Tyson on Unsplash

Unit-Testing

Unit-testing is a basic run of your code. It’s** not QA. This** way, you verify that you’ve written quality code and not something that will break the minute someone else tries to use it.

Why Test?

Recently, at a startup, I needed to write library code for a co-worker, I gave him the library code with a unit-test script.

Every time he complained, the unit-test saved me the time to check myself the bugs on his code. Since mine did work, this saves a lot of time, and time is your most valuable asset.

Image for post

Photo by insung yoon on Unsplash

Unit Testing is an essential aspect of development**. **Without the first runs of your code, how do you know if it works? Since unit-testing is often quick and short, by managing unit-testing correctly, you can see if you broke old code or someone else’s code!

Providing examples of how to use your code will be easy since you already have one!

The wrong and common way

Let us start by showing the wrong and conventional method. Here’s a simple class object:

As you can see, this class doesn’t do much. It takes a number and power that number on a given input. Now, what a standard developer might do to verify this code?

Image for post

#programming #data-science #python #development #unittest

Start Using Python’s Unittest Module Today
1.15 GEEK