Testing your package
1. Testing your package
Great work so far! You have completed every step required to transform some loose code into a package hosted on PyPI. In this chapter we are going to focus on bringing your code up to an even higher standard. And the first step on this journey is testing.2. The art and discipline of testing
When you are writing code, you will probably be performing small tests along the way to make sure it's working. Imagine you are writing this function to return the first and last element in a list. You might test it like this. This is the basic idea of testing, but in a good package, instead of just running this test once, you would save the code used to run this test. This means every time you make changes you can run all your tests again, to make sure your changes haven't broken anything.3. The art and discipline of testing
Good packages like to brag to users about how many tests they have. At the time of writing, 91 percent of the Pandas package is covered by tests. Writing tests will help you track down bugs, and also signals to your users that your package can be trusted to perform as intended.4. Writing tests
Each function in your package should have a test function. Here the test function for get-ends is defined. It runs the get-ends function on a list and makes sure get-ends returns the correct answer. If get-ends returns the correct answer then the test function passes.5. Writing tests
If something is wrong with get-ends, then this test function raises an assertion error.6. Writing tests
You can also include multiple assert statements to test your function in different ways.7. Organizing tests inside your package
You should keep these tests in their own directory which is in the top folder of the package.8. Organizing tests inside your package
The best way to lay out the tests directory is to copy the structure of the code directory. The test directory has an empty init file and the preprocessing subdirectory. Inside this subdirectory is the test-normalize module. The test-normalize module should contain all the tests for functions in the normalize module. Every other module in the code directory should have its own test module in the test directory.9. Organizing a test module
Inside the test module, there should be a test function for each function defined in the source module. Remember that you will need to import the functions you are testing from the main package. This should be done using an absolute import. In this course we are only going to use these simple assert statements for testing, but if your package has more complex functions you will need more complex tests. If you'd like to learn more, you can take this great course on writing more complex tests here on DataCamp.10. Running tests with pytest
Once you have written these tests you can run them all at once using pytest. From the terminal all you need to do is navigate to the top of your directory, and then run the pytest command. Pytest will look inside the test directory, and search for all modules that start with test-underscore. Inside those modules it will look for all functions that start with test-underscore, and it will run these functions.11. Running tests with pytest
The output for pytest will look something like this.12. Running tests with pytest
Pytest was run in the mysklearn directory and it found 6 test functions.13. Running tests with pytest
The test functions cover the normalize and standardize modules with 50 percent and 100% coverage of the code.14. Running tests with pytest
All 6 tests passed.15. Running tests with pytest
If a test fails then pytest will highlight this so you can fix it.16. Let's practice!
Alright, it's time make your package even better by adding tests. Let's practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.