0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【unittest】辞書型オブジェクトに期待するkeyが存在するかテスト

Last updated at Posted at 2022-02-24

Pythonでのunittestにおいて、辞書型のオブジェクトに期待するkeyが存在するかテストしたのでテスト方法をメモしておく。

テスト方法

辞書型オブジェクトに期待するkeyが存在するかテストするために、unittestassertIn()を使用した。

assertIn(a,b)

assertIn(a,b)は、aがbに含まれているかテストするメソッド。これを使ったサンプルが以下となる。

import unittest

class TestSample(unittest.TestCase):
    def test_sample1(self):
        test_data = {
            "name": "ryota",
            "score": 63,
        }
        self.assertIn("score", test_data)

    def test_sample2(self):
        test_data = {"name": "ryota", "score": {"Math": 63, "English": 50}}
        self.assertIn("Math", test_data["score"])
        # これだとダメ
        # self.assertIn("Math", test_data) 

if __name__ == "__main__":
    unittest.main()

また、assertIn(a,b)の否定がassertNotIn(a,b)となる。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?