Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
About Article
Analyze Data
Archive
Best Practices
Better Outputs
Blog
Code Optimization
Code Quality
Command Line
Course
Daily tips
Dashboard
Data Analysis & Manipulation
Data Engineer
Data Visualization
DataFrame
Delta Lake
DevOps
DuckDB
Environment Management
Feature Engineer
Git
Jupyter Notebook
LLM
LLM Tools
Machine Learning
Machine Learning & AI
Machine Learning Tools
Manage Data
MLOps
Natural Language Processing
Newsletter Archive
NumPy
Pandas
Polars
PySpark
Python Helpers
Python Tips
Python Utilities
Scrape Data
SQL
Testing
Time Series
Tools
Visualization
Visualization & Reporting
Workflow & Automation
Workflow Automation

Testing

ipytest: Unit Tests in IPython Notebooks

It is important to create unit tests for your functions to make sure they work as you expected, even the experimental code in your Jupyter Notebook. However, it can be difficult to create unit tests in a notebook.

Luckily, ipytest allows you to run pytest inside the notebook environment. To use ipytest, simply add %%ipytest -qq inside the cell you want to run pytest.

Link to ipytest.

My previous tips on Jupyter Notebook.

ipytest: Unit Tests in IPython Notebooks Read More »

Deepchecks: Check Category Mismatch Between Train and Test Set

Sometimes, it is important to know if your test set contains the same categories in the train set. If you want to check the category mismatch between the train and test set, use Deepchecks’s CategoryMismatchTrainTest.

In the example above, the result shows that there are 2 new categories in the test set. They are ‘d’ and ‘e’.

Link to Deepchecks.

My previous tips on testing.

Deepchecks: Check Category Mismatch Between Train and Test Set Read More »

hypothesis: Property-based Testing in Python

If you want to test some properties or assumptions, it can be cumbersome to write a wide range of scenarios.

To automatically run your tests against a wide range of scenarios and find edge cases in your code that you would otherwise have missed, use hypothesis.

In the code above, I test if the addition of two floats is commutative. The test fails when either x or y is NaN. Now I can rewrite my code to make it more robust against these edge cases.

Learn more about hypothesis here.

Link to my previous tips about testing.

hypothesis: Property-based Testing in Python Read More »

pytest-sugar: Show the Failures and Errors Instantly With a Progress Bar

It can be frustrating to wait for a lot of tests to run before knowing the status of the tests. If you want to instantly see the failures and errors with a progress bar, use pytest-sugar.

pytest-sugar is a plugin for pytest. The code above shows how the outputs will look like when running pytest.

Link to pytest-sugar.

Link to previous tips on testing in Python.

pytest-sugar: Show the Failures and Errors Instantly With a Progress Bar Read More »

DeepDiff Find Deep Differences of Python Objects

When testing the outputs of your functions, it can be frustrated to see your tests fail because of something you don’t care too much about such as:

order of items in a list

different ways to specify the same thing such as abbreviation

exact value up to the last decimal point, etc

Is there a way that you can exclude certain parts of the object from the comparison? That is when DeepDiff comes in handy.

Link to DeepDiff.

Link to my previous tips on testing.

DeepDiff Find Deep Differences of Python Objects Read More »

pytest: Don’t Assume. Test your Python Code

When working on a data science project, don’t assume that the output will be what you expect. Test your assumption. The easiest way to do that in Python is pytest.
For example, in the code above, I use pytest to test my assumption about the output of the text. When running pytest file_name.py, pytest will run the functions that start with test_.
By running the test, I know where can my function goes wrong.
Check out other examples in my article about pytest.

pytest: Don’t Assume. Test your Python Code Read More »

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran