1
さまざまなストレージバックエンドを使用できるストレージインターフェイスを作成しました。デモンストレーションとして、オブジェクトを標準のPython辞書にキーと値のペアとして格納する実装を記述しました。コレクションインタフェースを実装するユニットテストクラス?
class MyStorageInterface(object):
def store(self, key, value):
raise NotImplementedError("Class must be subclassed")
def get(self, key):
raise NotImplementedError("Class must be subclassed")
# Example implementation
class DictStorage(MyStorageInterface):
def __init__(self):
self._cache = dict()
def store(self, key, value):
self._cache[key] = value
def get(self, key):
return self._cache[key]
ここで、DictStorage
実装のための単体テストを書きたいと思います。
storage = DictStorage()
value = 8
key = 'foo'
storage.store(key, value)
actual = storage.get(key)
assert actual == 8, "cache value for %s is %s, expected %s" % (key, str(actual), str(value))
は、getメソッドまたはコレクション型のオブジェクトを実装、テストクラスのPython的パターンがあるかどうか、適切なユニットテストです:私の質問のようなものかどうかではありません。