mambaを利用してpythonでBDD
pythonで利用できるBDDのツールに良いのが無いよなぁと思っていたら、きちんと awesome-python に記載されていた。
Mamba — mamba 0.9.2 documentation
個人的な所感としては割と良い気がする。RubyのRspecの雰囲気を感じ取れる。
インストール
pipenv install mamba
書き方
with
句と mamba
で用意されている description
もしくは describe
, context
, it
と、 expects
というパッケージに用意されている expect
などを利用して普通のBDDがpythonでも書ける感じ。
_context
などのアンダースコアで始まるのはペンディングの印
全く意味がないテストコードだけど、以下の様。
from mamba import describe, context, it, _context, _it from expects import expect, be_true, equal from faker import Faker # サンプルのデータを作るやつ fake = Faker() fake.name() with describe("New topi") as self: with context("Bool"): with it("True"): expect(True).to(be_true) with describe("Pendins") as self: with _context("penndins"): with it("pending"): pass with describe("Pendins") as self: with context("penndins"): with _it("pending"): pass class Human(object): def __init__(self, name): self._name = name @property def name(self): return self._name with describe("Human") as self: with before.each: self.human = Human(fake.name()) with it("has name"): expect(type(self.human.name)).to(equal(str))
ライフサイクル
ライフサイクルとしては以下のようになっている
- before.all
- (describe内の) テストを実行する前に1度だけ
- before.each
- (describe内の) 各テストを実行する前に毎回
- after.each
- (describe内の) 各テストを実行したあとに毎回
- after.all
- (describe内の) テストを実行した後に1度だけ
参考は以下
Hooks — mamba 0.9.2 documentation
実行方法
pipenv run mamba **/*_spec.py
とかで実行可能
また pipenv run mamba --enable-coverage
と実行した後に、 pipenv run coverage html
でカバレッジを見ることができる。