Prologue
Every programming language has a large number of tools for testing the code. And so is Python. However, with the Pandas library, this is not really common. In the article, the author also mentioned two reasons because Pandas can be more difficult to test normal Python code syntax or it is more commonly used by experts than programmers.
However, testing is a phase in software development so it is not advisable to know some tools to ease this process.
Okay, and today’s translation on a fairly new topic is just getting started!!!
Content
Using Pytest
Pytest is a very popular and simple tool for testing code. For some other tools, it requires defining classes for each test pass, which inadvertently makes this a bit more cumbersome. With Pytest it’s simpler and easier.
Install Pytest
To use it, we can use the command:
1 2 | pip install pytest |
You can learn more about it at Pytest
Example with Pytest
For example, I want to test the code to calculate the sum of the following two numbers:
1 2 3 4 | # operations.py def sum_a_and_b(a, b): return a + b |
I write the code in the file <name>.py (eg operations.py …).
To test the above code, I performed the following steps:
- Write the above code in a .py file (eg operations.py )’
- Create another .py file to test the above code (eg test_operations.py)
- Write the code to test the function sum_a_and_b(a, b) :
- Import the sum function from the file operations(a, b)12from operations import sum_a_and_b as sum
- Write a function to test the sum function:12345def test_sum():result = sum_a_and_b(1, 2)assert result == 3
- Run the test file with the terminal:
- cd into the directory containing the file testing
- Run the code in the test file with type pytest
- Check the results
- Import the sum function from the file operations(a, b)
You can learn more about some of the strengths of pytest in this course Python Automatic Pytest
How to write a standard test
Based on the author’s opinion
Beyond the Happy Path
The test version Happy path is the test version 1 which is usually the version we expect first. This is the simplest case and the problem we need the first solver function for. However, we also need cases called the edge cases to evaluate the function 100%.
Testing Principles
For unit tests, it’s a good rule of thumb to remember that tests should always follow the common “arrange/act/assert” structure.
The steps include:
- arrange : things needed for testing like creating necessary data, preparing in-memory database or mocking API calls…
- act: function or method to call it
- assert: expected output.
‼️ Note that, if you want to unit test with user interaction, you can use the following three types: given/when/then
- Given: what you need to run the test
- When: interactive
- Then: expected outcome
Take Your Time Naming Everything Correctly
References:
- Pandas Code Testing Guide for New Python Developers – https://hackernoon.com/pandas-code-testing-guide-for-new-python-developers
- Home page: https://pypi.org/project/pytest/
Thank you
First of all, I would like to thank Mr. Eduardo Motta de Morae – the author of the article. Thank you all for reading my translation. Thanks a lot.