evkline
evkline

Reputation: 1511

Is there a preferred BDD style unit-testing framework for Python?

I was wondering if there are any BDD-style 'describe-it' unit-testing frameworks for Python that are maintained and production ready. I have found describe, but it doesn't seem to be maintained and has no documentation. I've also found sure which reached 1.0, but it seems to just add syntactic sugar instead of writing assertions. What I'm really looking for is something similar to RSpec and Jasmine that enables me to setup test suites. The describe-it syntax that allows for testing multiple cases of a function. Versus a classical assertion structure that tests each function once and has multiple assertions for testing multiple cases. This breaks the isolation of a unit test. If there's a way to achieve something similar with the assertion-style testing I'd appreciate any advice on how to do it. Below are simple examples of both styles:

foo.py

class Foo():
    def bar(self, x):
        return x + 1

BDD-Style/Describe-It

test_foo.py

describe Foo:
    describe self.bar:
        before_each:
            f = Foo()

        it 'returns 1 more than its arguments value':
            expect f.bar(3) == 4

        it 'raises an error if no argument is passed in':
            expect f.bar() raiseError

Unittest/assertion-style

test_foo.py

 class Foo():
     def test_bar(x):
         x = 3
         self.assertEqual(4)
         x = None
         self.assertRaises(Error)

Upvotes: 4

Views: 2485

Answers (2)

Nick Vanbaelen
Nick Vanbaelen

Reputation: 111

I've been looking for this myself and came across mamba. In combination with the fluent assertion library expects it allows you to write BDD-style unit tests in Python that look like this:

from mamba import describe, context, it
from expects import *

with describe("FrequentFlyer"):
    with context("when the frequent flyer account is first created"):
        with it("should initially have Bronze status"):
            frequentFlyer = FrequentFlyer()
            expect(frequentFlyer.status()).to(equal("BRONZE"))

Running these tests with documentation formatting gives you a Jasmine like test report:

> pipenv run mamba --format=documentation frequent_flyer_test.py

FrequentFlyer
  when the frequent flyer account is first created
    ✓ it should initially have Bronze status

1 example ran in 0.0345 seconds

Upvotes: 2

hspandher
hspandher

Reputation: 16733

If you are expecting something exactly like rspec/capybara in python, then I am afraid you are in for a disappointment. The problem is ruby provides you much more freedom than python does (with much more support for open classes and extensive metaprogramming). I have to say there is a fundamental difference between philosophy of python and ruby.

Still there are some good testing frameworks like Cucumber (https://github.com/cucumber/cucumber/wiki/Python) and lettuce (http://lettuce.it/) in case you are looking for pure python solution.

Upvotes: 1

Related Questions