Intermediate Python

Testing in Python

Intermediate Python

Check out a free preview of the full Intermediate Python course

The "Testing in Python" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina gives an introduction to unit tests, integration tests, and functional tests. The 'assert' keyword's functionality is also demonstrated as a sanity check.

Preview
Close

Transcript from the "Testing in Python" Lesson

[00:00:00]
>> Nina Zakharenko: We're gonna talk about testing in Python. Tests are a cornerstone of a solid Python program. And thankfully, because of Python's batteries included philosophy, everything that we need to write unit tests is included in the standard library. So let's learn about the basics of unit testing. Unit testing is a method of testing software methods where individual functions are tested in an automatic fashion to determine if they work as expected.

[00:00:33]
These automated tests, they help you find bugs quicker and faster than not testing all, or even manual testing. And you can also write them alongside, or before you write your functions, in a style called test-driven development to help your code be as bug free as possible. There are three types of automated tests that can be performed at different levels of abstraction.

[00:01:02]
Unit tests operate on a small testable unit of code, usually a function, that does a single action or an operation. Integration tests will check to see if different units or modules of code work together as a group. And then functional tests operate on units of functionality to make sure that a specific function of a software is working.

[00:01:27]
And it may involve a lot of unit tests or whole systems working together.
>> Nina Zakharenko: For this class, I'm just gonna show you how to write unit tests. But know that tests in the real world are pretty essential for your Python production programs. They're very helpful because many companies use a style of development called CI/CD, or continuous integration/continuous deployment.

[00:01:54]
That means a lot of unit tests, integration tests, maybe even a few functional tests, which run automatically before your code is merged in with the deployed master branch. If the tests fail, the deployment can be stopped or not happen at all. The developers can be notified. And your broken code doesn't make it into your production servers.

[00:02:22]
But this technique only works if you write a lot of good tests. It can be a bit complicated to set this appropriately, depending on which deployment strategy you're using. But it really saves you an enormous amount of time in the long run and helps your users to not see bugs.

[00:02:43]
>> Nina Zakharenko: So before we talk about unit tests themselves, Python comes with something called the assert keyword. And that kinda helps you do a little bit of quick sanity checking. An assertion just checks a boolean value of a condition to see if it's true or false, if it matches.

[00:03:08]
If the assertion is true, the program will continue. If it's false, you'll see an assertion error and your program will stop. So let's say, I'm going back to my REPL,
>> Nina Zakharenko: Let's say I have a number a. I can type assert > 0. And that passes just fine.

[00:03:31]
If I assert that a > 0, I'm gonna get an assertion error. Know that assertion errors are for sanity checking. They're not for production. Simple checks, sanity checks, they're not for validation. Certainly not for validating any type of user input or determining if a file isn't found, something like that.

[00:03:58]
These situations are better suited for raising an actual exception. And the other thing about assertions is they're not guaranteed to be run in your production code. It's easy to turn off, disable all assertions at run time, just by starting up your Python program with a particular flag. So never rely on assertions to run on production code.

[00:04:23]
Never use them for validation. Never use them for user input validation. Make sense?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now