Python Tutorial: More benefits and test types

preview_player
Показать описание

---
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_to_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_to_list() 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 code base 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 upon, we must write unit tests and setup 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 2, we will dig much deeper into pytest and write more advanced unit tests for the features and models module. Exciting!

But for now, let's practice the concepts in this lesson.

#Python #PythonTutorial #DataCamp #Unit #Testing #DataScience #pytest
Рекомендации по теме
join shbcf.ru