A simple Python TDD example using:
- chef (for provisioning)
- vagrant (for development environment)
- expects (for assertions)
- doublex (for mocking and method-call assertions)
- doublex-expects (for more expectation + assertion goodness)
- mamba (for BDD testing)
# Clone the repo:
git clone [email protected]:jdrago999/python-tdd-with-mamba.git
cd python-tdd-with-mamba/
# Create the Vagrant VM:
vagrant up
# Enter the VM:
vagrant ssh
# Run tests:
cd /var/www/python-tdd-with-mamba
mamba
## File: spec/foo_spec.py
from expects import *
import doublex
from expects.testing import failure
from doublex_expects import *
import sys
sys.path.insert(0, 'foo')
from foo import Foo
with description(Foo):
with description('#bar'):
with context('when data'):
with context('is good'):
with before.each:
global baz
baz = {'msg': 'good data'}
with it('returns True'):
foo = Foo()
expect(foo.bar(baz)).to(be_true)
with context('is bad'):
with before.each:
global baz
baz = {'msg': 'something invalid'}
with it('returns False'):
foo = Foo()
expect(foo.bar(baz)).to(be_false)
with description('#bux(name)'):
with context('when name ends with'):
with context('a vowel'):
with it('returns 0'):
foo = Foo()
expect(foo.bux('viola')).to(equal(0))
with context('a consonant'):
with it('returns 1'):
foo = Foo()
expect(foo.bux('cats')).to(equal(1))
## File: foo/foo.py
import re
class Foo:
def bar(self, msg):
if msg['msg'] == 'good data':
return True
else:
return False
def bux(self, name):
if re.compile(r".*?[aeiouy]$", re.I).match(name):
return 0
else:
return 1
Mamba already comes with coverage
, so just:
mamba --enable-coverage
coverage report -m | tail -n 1 | awk '{print $6}'
If that prints 100%
then you're finished. Otherwise, keep adding tests.
coverage html
will produce a nice report for you like the one below: