有没有首选的BDD风格的Python单元测试框架? [英] Is there a preferred BDD style unit-testing framework for Python?

查看:94
本文介绍了有没有首选的BDD风格的Python单元测试框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有维护和生产就绪的BDD风格的描述"单元测试框架.我已经找到描述,但是它似乎没有得到维护并且没有文档.我还发现 sure 达到1.0,但它似乎只是添加了语法糖而不是编写断言.我真正要寻找的是类似于RSpec和Jasmine的东西,这些东西使我能够设置测试套件. describe-it语法允许测试功能的多种情况.与经典的断言结构相比,该结构只测试每个函数一次,并具有用于测试多个案例的多个断言.这破坏了单元测试的隔离.如果有一种方法可以通过断言风格的测试来实现类似的目的,那么我将不胜感激如何使用它的建议.下面是这两种样式的简单示例:

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样式/描述

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

单元测试/断言风格

test_foo.py

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

推荐答案

我一直在寻找自己,遇到了期望,它允许您在Py​​thon中编写看起来像BDD样式的单元测试像这样:

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"))

以文档格式运行这些测试会为您提供类似Jasmine的测试报告:

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

这篇关于有没有首选的BDD风格的Python单元测试框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆