Python Tutorial: More benefits and test types

Unit tests document code behavior through assert statements and build user trust via coverage badges. Continuous integration rejects failing changes before production. The tutorial covers unit tests for isolated functions, integration tests for multiple modules working together, and end-to-end tests for entire pipelines, using a housing-price project as an example.
Chapters
In a previous lesson, we learned that unit tests save a lot of time, but the benefits of unit testing goes beyond time savings. The unit tests that we wrote for row 2 list also serve as documentation. If a collaborator didn't know this function's purpose, they could recreate the argument and return value table on the right by looking at the boolean expressions used in the assert statements. The table would give them a good hint about what row toolist does, helping them understand the function's code faster. To mimic this real life situation, some exercises may ask you to guess a function's job by looking at a test module. In this case, just type exclamation cat followed by the test module Name in the IPython console to see the unit tests. Unit tests also increase trust in a package, as users can run the unit tests and verify that the functions work. In the picture we see NumPy's GitHub page. This highlighted badge shows how much of the codebase is unit tested, and this other badge shows whether the tests are passing. Users trust Numpy more because of these badges. We should implement these badges for our projects too, because user trust is important and we will learn how to do that later in the course. Unit tests can also reduce downtime for a productive system Suppose we make a mistake and push bad code to a productive system. This will bring the system down and annoy users. We can cure this by setting up continuous integration or CI. CI runs all unit tests when any code is pushed, and if any unit test fails, it rejects the change, preventing downtime. It also informs us that the code needs to be fixed. If we run productive systems that many people depend on, we must write unit tests and set up CI. We will see an example of this later in the course. All of these benefits make the case stronger for writing unit tests. In this course we will write unit tests for all functions. In the example linear regression project, we already wrote tests for row to list and convert to int. They are part of the data module which creates a clean data file from raw data on housing area and price. Very soon we will see functions from the feature module which compute features from the clean data. Later in the course we will meet the Models module which will output a model for predicting housing price from the features. The tests that we wrote are called unit tests because they test a unit such as row to list. A unit is any small independent piece of code and could be a Python function or class. In contrast, integration tests check if multiple units work well together when they are connected and not just independently. For example we could check if the data and the feature module work well. When connected here, the argument will be the raw data and the return values to check would be the features. End to end tests check the whole software at once. They start from one end, which is the unprocessed data file, goes through all the units till the other end and checks whether we get the correct model. This course is focused on unit tests, as this is really the best way to learn about pytest. In chapter two, we will dig much deeper into pytest and and write more advanced unit tests for the features and models module. Exciting. But for now, let's practice the concepts.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







