Pythonパッケージのテストを書く#
Writing code that tests your package code, also known as test suites, is important for you as a maintainer, your users, and package contributors. Test suites consist of sets of functions, methods, and classes that are written with the intention of making sure a specific part of your code works as you expected it to.
なぜパッケージのテストを書くのか?#
Tests act as a safety net for code changes. They help you identify and fix bugs before they affect users. Tests also instill confidence that code changes from contributors won't break existing functionality.
Pythonパッケージのテストを書くことは重要です:
Catch mistakes: Tests are a safety net. When you make changes or add new features to your package, tests can quickly tell you if you accidentally broke something that was working fine before.
Save time: Imagine you have a magic button that can automatically check if your package is still working properly. Tests are like that magic button! They can run all those checks for you, saving you time.
Easier collaboration: If you're working with others or have outside contributors, tests help everyone stay on the same page. Your tests explain how your package is supposed to work, making it easier for others to understand and contribute to your project.
Fearless refactoring: Refactoring means making improvements to your code structure without changing its behavior. Tests empower you to make these changes; if you break something, test failures will let you know.
Documentation: Tests serve as technical examples of how to use your package. This can be helpful for new technical contributors who want to contribute code to your package. They can look at your tests to understand how parts of your code functionality fits together.
Long-term ease of maintenance: As your package evolves, tests ensure that your code continues to behave as expected, even as you make changes over time. Thus you are helping your future self when writing tests.
より簡単なプルリクエストのレビュー: GitHub Actionsのような CI フレームワークでテストを実行することで、あなたや貢献者がコードベースに変更を加えるたびに、コードベースの問題や変更点をキャッチすることができます。 これにより、あなたのソフトウェアが期待通りに動作することが保証されます。
ユーザーエッジケースのテスト#
エッジケースとは、あるユーザがあなたのパッケージを使用する際の、予期せぬ、あるいは "外れ値" の使い方を指します。 テストによって、パッケージの機能を損なう可能性のある様々なエッジケースに対処することができます。 例えば、ある関数がpandasの dataframe を期待したのに、ユーザがnumpyの array を提供した場合、何が起こるでしょうか? あなたのコードは、このような状況を丁寧に処理し、明確なフィードバックを提供していますか?それとも、原因不明の失敗でユーザーをイライラさせたままにしていますか?
注釈
テストの入門書としては、 このSoftware Carpentryのレッスン を参照してください。
テスト例
Let's say you have a Python function that adds two numbers together.
def add_numbers(a: float, b: float) -> float:
"""
Add two numbers together and return the result.
Parameters
----------
a : float
The first number to add.
b : float
The second number to add.
Returns
-------
float
The sum of the two numbers.
"""
return a + b
異なる数値が与えられたときに、その関数が期待通りに実行されることを確認するテストは次のようになります:
def test_add_numbers():
result = add_numbers(2, 3)
assert result == 5, f"Expected 5, but got {result}"
result2 = add_numbers(-1, 4)
assert result2 == 3, f"Expected 3, but got {result2}"
result3 = add_numbers(0, 0)
assert result3 == 0, f"Expected 0, but got {result3}"
test_add_numbers()
🧩🐍 How do you know what type of tests to write?#
As you begin to write tests for your package, you should consider:
there are three types of tests Test Types for Python Packages that can help guide your development.
your tests should consider how a user might use (and misuse!) your package.
注釈
This section has been adapted from a presentation by Nick Murphy.
But, what should you be testing in your package? Below are a few examples:
いくつかの典型的なケースをテスト: パッケージが、ユーザーが使用したときに期待通りに機能することをテストします。例えば、パッケージが2つの数値を足すことになっている場合、その2つの数値を足した結果が正しいかどうかをテストします。
Test special cases: Sometimes there are special or outlier cases. For instance, if a function performs a specific calculation that may become problematic closer to the value of 0, test it with the input of both 0 and nearby values.
Test at and near expected boundaries: If a function requires a value that is greater than or equal to 1, make sure that the function still works with the values 1 and 0.999, as well as 1.001 (values close to the constraint). Make sure that the function fails gracefully when given unexpected values and that the user can easily understand why it failed by providing a useful error message.
Next steps#
Now that you understand what and why to test, explore the three types of tests (unit, integration, and end-to-end) to determine which style of tests best fits your package. Then, learn how to run your tests locally and in continuous integration. Finally, track your progress with code coverage metrics.