Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Open Source Conference 2022 Online Osaka

Open Source Conference 2022 Online Osaka

Pythonのユニットテスト用ライブラリHypothesisの紹介

Ryo Murakami

January 29, 2022
Tweet

More Decks by Ryo Murakami

Other Decks in Programming

Transcript

  1. Python のユニットテスト用ライブラリ
    Hypothesis の紹介
    村上 涼 / PyCon JP
    2022/01/29 Open Source Conference 2022 Online Osaka

    View Slide

  2. 自己紹介
    村上 涼
    ● セキュリティエンジニア
    ● PyCon JP 2021 スタッフ
    ● Python, C++, Go, TypeScript, …
    ● @kyomukyomupurin

    View Slide

  3. 自己紹介
    ● 『サイバーセキュリティプログラミング 第2版』
    ○ 2022/04/27 発売(予定)
    ○ Amazon

    View Slide

  4. 目次
    ● ユニットテスト
    ● Hypothesis

    View Slide

  5. 目次
    ● ユニットテスト
    ● Hypothesis

    View Slide

  6. ユニットテスト
    ● example-based testing
    ○ unittest, pytest
    ● property-based testing
    ○ Hypothesis

    View Slide

  7. ユニットテスト
    例:連長圧縮(Run Length Encoding)
    ● encoder
    ○ “aabbbcccc” → [(“a”, 2), (“b”, 3), (“c”, 4)]
    ● decoder
    ○ [(“a”, 2), (“b”, 3), (“c”, 4)] → “aabbbcccc”

    View Slide

  8. ユニットテスト
    ● example-based testing
    ○ unittest, pytest
    ● property-based testing
    ○ Hypothesis

    View Slide

  9. example-based testing
    ● よくあるテスト
    ● 何らかの基準で入力値を用意し、期待される出力値が得られるかどうかチェック

    View Slide

  10. example-based testing
    import unittest
    class Tester(unittest.TestCase):
    def test_encoder(self):
    self.assertEqual(encoder(“abc”), [(“a”, 1), (“b”, 1), (“c”, 1)])
    self.assertEqual(encoder(“aaabbbb”), [(“a”, 3), (“b”, 4)])
    self.assertEqual(encoder(“aaaaa”), [(“a”, 5)])
    def test_decoder(self):
    self.assertEqual(decoder([(“a”, 1), (“b”, 1), (“c”, 1)]), “abc”)
    self.assertEqual(decoder([(“a”, 3), (“b”, 4)]), “aaabbbb”)
    self.assertEqual(decoder([(“a”, 5)]), “aaaaa”)

    View Slide

  11. example-based testing
    import unittest
    class Tester(unittest.TestCase):
    def test_encoder(self):
    self.assertEqual(encoder(“abc”), [(“a”, 1), (“b”, 1), (“c”, 1)])
    self.assertEqual(encoder(“aaabbbb”), [(“a”, 3), (“b”, 4)])
    self.assertEqual(encoder(“aaaaa”), [(“a”, 5)])
    def test_decoder(self):
    self.assertEqual(decoder([(“a”, 1), (“b”, 1), (“c”, 1)]), “abc”)
    self.assertEqual(decoder([(“a”, 3), (“b”, 4)]), “aaabbbb”)
    self.assertEqual(decoder([(“a”, 5)]), “aaaaa”)

    View Slide

  12. ユニットテスト
    ● example-based testing
    ○ unittest, pytest
    ● property-based testing
    ○ Hypothesis

    View Slide

  13. property-based testing
    ● あらゆる入力と対応する出力について成り立つ特性(
    property)をひとつ設定
    ● テストケースはフレームワークがランダムに生成

    View Slide

  14. property-based testing
    from hypothesis import given
    from hypothesis import strategies as st
    @given(st.text())
    def tester(s: str):
    assert decoder(encoder(s)) == s

    View Slide

  15. property-based testing
    from hypothesis import given
    from hypothesis import strategies as st
    @given(st.text())
    def tester(s: str):
    assert decoder(encoder(s)) == s
    property

    View Slide

  16. example-based testing vs property-based testing
    ● 状況に応じて使い分ける
    example-based testing property-based testing
    書きやすさ ○ △
    バグの検出力 △ ○

    View Slide

  17. 目次
    ● ユニットテスト
    ● Hypothesis

    View Slide

  18. Hypothesis
    ● Python で property-based testing を実施するためのフレームワーク
    ● 開発が活発
    ● ドキュメントが充実

    View Slide

  19. Hypothesis
    使い方
    ● テスト対象の特性(property)を設定し、コード化
    ● 入力の型を設定
    ○ text(), integers(), booleans(), lists(), tuples(), ip_addresses(), timezones()…
    ○ 例:@given(st.lists(st.text()))
    ● 実行

    View Slide

  20. Hypothesis
    エッジケース

    View Slide

  21. Hypothesis
    “shrinking”
    ● 単にエッジケースを見つけるだけではなく、
    より小さくよりシンプルなケースに変換する
    ● 決定性有限オートマトンを学習するアルゴリズム
    ○ 文字列はエッジケースの性質を保ったままより短く
    ○ 数字はエッジケースの性質を保ったままより小さく

    View Slide

  22. まとめ
    ● property-based testing はユニットテストの手法のひとつ
    ● 特性を設定し、具体的なテストケースはフレームワークが作成
    ● Hypothesis は Python で property-based testing を実施するためのライブラリ
    ● 網羅性と “shrinking”

    View Slide

  23. 参考
    ● Welcome to Hypothesis!
    ● GitHub - Hypothesis
    ● Property-based testing: what is it?
    ● gopterでステートフルなPBT

    View Slide