Learn Python 3 Unit Testing

Created: 2021-10-17 17:00 Ireland Time

Last Updated: 2022-07-13 – Added new video

In here I teach you Unit Testing for Python 3, from the scratch in videos of around 5 minutes each.

However on 2022-07-12 I streamed on my Twitch channel a 65 minutes live session in 4K creating Unit Testing Code from the scratch, I add it here:

2022-07-12 Video teaching Unit Testing from zero

For the next series of videos I start with a very simple code and I evolve it, I refactor, and I add Unit Testing, first with Python 3, later with pytest Framework.

Set Youtube’s max quality, I recorded it to 1080p Full HD, from the Setting icon on bottom right to see the screen clearly, so you should choose 1080p.

Subtitles/CC are auto generated, but are very precise.

I found this nice tutorial about Unit Testing from Real Python that you may want to check as well:


Lesson 0 (05:52)

A brief introduction to Unit Testing and to why we use it.

An introduction to MT Notation, the Notation I invented, and why to use.

We see why using the Integer Division we are providing not very exact values. Also truncating versus round is giving us different values.

Lesson 1 (05:56)

Wee add our first assert to test a condition.

We refactor: extract functionality to a function.

Simplifying the logic.

We see how the print with formatting shows a value different from what we expect.

Lesson 2 (5:08)

In this video we:

  • Extract the functionality to calculate free percent for a function.
  • We see docstring comments.
  • We add, and see why is important, to use if __name__ == “__main__”:

Lesson 3 (6:54)

In this video we:

  • Extract more functionality, and create a function get_gb_with_decimals(f_gb) to return a float truncated, not rounded, to the number of positions specified.
  • We add more assert assertions.
  • We use expected numeric results, instead of the variable number of GB that each computer has, to Test the Code.
  • Thanks to the assert we see how round provides a different value versus what we were expecting.
  • We face a judgement call for the Test: what is acceptable for us?.
  • The Tests make us realize that there are problems that we didn’t see before.

Lesson 4 (08:38)

  • We implement our own truncate function to truncate n decimal positions in floats.
  • Don’t use reserved words.
  • I use a trick for truncate: convert Float to String, and look for the dot ‘.’
  • We see the Python3 assert doesn’t show exactly what were the values in both sides of the assertion when there is an error (pytest do)
  • The Tests catch an error I didn’t realize.
  • I create intermediate variables to make the code more clear.

Lesson 5 (19:09)

In here I’m tired after all day work, so I’m a bit more slow, and I make some mistakes that after I fix. MT Notation prefix variable system helps me to catch them.

I address the code. I clean code not needed anymore.

I rename i_total_gb_with_decimals to f_total_gb_with_decimals, as it is a Float.

I explain how to scroll fast using CTRL + Cursor key (Right or Left) for Windows and Linux.

I show how "%f" % f_value in Strings is for representing floats, as "%d" % i_value is for Integers.

We convert the Float to String and set the basis for returning the messages from functions, so we can test the output messages.

I show how to solve a difficult conversion case.

I show how to do the assertions, with the full value expected hard coded, and not with replacements in there that can bias the Test.

I check the corner cases 0.0000 and 467. to be converted.

Me make the case 0.00 GiB to be displayed as 0 GiB.

Lesson 6 (10:01)

In this video I:

  • Configure the Virtual Environment in PyCharm
  • Add package pytest
  • Explain how the names of the files and of the Test Functions will be
  • Run our First Test
  • Show Code Coverage

Lesson 7 (11:10)

I raise the Code Coverage to 100% to functions.

Also I found that Python accepts the conversion of a number with dot and nothing else to float, like float("100."). This will return 100.0 but will not trigger any error.

In other languages it would.

I also introduce the concept of fixture.

Lesson 8 (5:55)

I show you how to add complete Code Coverage, and show how to use the Fixture capsys to capture Standard Output and Standard Error.

In introduce the reasons why we need to decouple things and I introduce the concept of Dependency Injection.

Lesson 9 (26:00) Dependency Injection, Monkey Patch

I show the use of Dependency Injection, and why we needed it.

I show Monkey Patching in action.

I finally do the Test checking the exact expected output as we know what we should get (thanks to having monkeypatched it).

Lesson 10 (16:26) Refactoring a Method to extract functionality, and testing with capsys fixture

First I show the difficulties to add Unit Testing to coupled code, and how it is convenient to refactor extract.

I refactor over the project carleslibs branch 1.0.9 class MenuUtils to extract functionality coupled, and I test it using the pytest capsys fixture.

Views: 2,900 views

Rules for writing a Comment

  1. Comments are moderated
  2. I don't publish Spam
  3. Comments with a fake email are not published
  4. Disrespectful comments are not published, even if they have a valid point
  5. Please try to read all the article before asking, as in many cases questions are already responded

Leave a Reply