Every data science project needs unit testing. It comes with huge benefits - saving a lot of development and maintenance time, improving documentation, increasing end-user trust and reducing downtime of productive systems. As a result, unit testing has become a must-have skill in the industry, used by almost every company. This course teaches unit testing in Python using the most popular testing framework pytest. By the end of this course, you will have written a complete test suite for a data science project. In the process, you will learn to write unit tests for data preprocessors, models and visualizations, interpret test results and fix any buggy code. You will also learn advanced concepts like TDD, test organization, fixtures and mocking so that you can test your own data science projects properly.
Unit testing basicsFree
In this chapter, you will get introduced to the pytest package and use it to write simple unit tests. You'll run the tests, interpret the test result reports and fix bugs. Throughout the chapter, we will use examples exclusively from the data preprocessing module of a linear regression project, making sure you learn unit testing in the context of data science.Why unit test?50 xpHow frequently is a function tested?50 xpManual testing100 xpWrite a simple unit test using pytest50 xpYour first unit test using pytest100 xpRunning unit tests50 xpUnderstanding test result report50 xpWhat causes a unit test to fail?50 xpSpotting and fixing bugs100 xpMore benefits and test types50 xpBenefits of unit testing50 xpUnit tests as documentation50 xp
Intermediate unit testing
In this chapter, you will write more advanced unit tests. Starting from testing complicated data types like NumPy arrays to testing exception handling, you'll do it all. Once you have mastered the science of testing, we will also focus on the arts. For example, we will learn how to find the balance between writing too many tests and too few tests. In the last lesson, you will get introduced to a radically new programming methodology called Test Driven Development (TDD) and put it to practice. This might actually change the way you code forever!Mastering assert statements50 xpWrite an informative test failure message100 xpTesting float return values100 xpTesting with multiple assert statements100 xpTesting for exceptions instead of return values50 xpPractice the context manager100 xpUnit test a ValueError100 xpThe well tested function50 xpTesting well: Boundary values100 xpTesting well: Values triggering special logic100 xpTesting well: Normal arguments100 xpTest Driven Development (TDD)50 xpTDD: Tests for normal arguments100 xpTDD: Requirement collection100 xpTDD: Implement the function100 xp
Test Organization and Execution
In any data science project, you quickly reach a point when it becomes impossible to organize and manage unit tests. In this chapter, we will learn about how to structure your test suite well, how to effortlessly execute any subset of tests and how to mark problematic tests so that your test suite always stays green. The last lesson will even enable you to add the trust-inspiring build status and code coverage badges to your own project. Complete this chapter and become a unit testing wizard!How to organize a growing set of tests?50 xpPlace test modules at the correct location50 xpCreate a test class100 xpMastering test execution50 xpOne command to run them all100 xpRunning test classes100 xpExpected failures and conditional skipping50 xpMark a test class as expected to fail100 xpMark a test as conditionally skipped100 xpReasoning in the test result report100 xpContinuous integration and code coverage50 xpBuild failing50 xpWhat does code coverage mean?50 xp
Testing Models, Plots and Much More
In this chapter, You will pick up advanced unit testing skills like setup, teardown and mocking. You will also learn how to write sanity tests for your data science models and how to test matplotlib plots. By the end of this chapter, you will be ready to test real world data science projects!Beyond assertion: setup and teardown50 xpUse a fixture for a clean data file100 xpWrite a fixture for an empty data file100 xpFixture chaining using tmpdir100 xpMocking50 xpProgram a bug-free dependency100 xpMock a dependency100 xpTesting models50 xpTesting on linear data100 xpTesting on circular data100 xpTesting plots50 xpGenerate the baseline image100 xpRun the tests for the plotting function100 xpFix the plotting function100 xpCongratulations50 xp
Dibya ChakravortySee More
Senior Python Developer, TECH-5
Dibya is currently developing a test automation framework for a leading German car manufacturer. He thinks that high-quality, well-tested code is far more valuable than code that only seems to work.
His other passion is Deep Reinforcement Learning, because it is a step towards Strong AI.
Want to connect with him? Here is his LinkedIn profile.