用python练习BDD

JtR*_*JtR 132 python testing bdd

哪些是最先进的框架和工具,可用于python实践行为驱动开发?特别是找到类似rspec和mocha的类似工具的红宝石会很棒.

小智 48

生菜意味着成为一个类似黄瓜的python工具:http://lettuce.it/

您可以在github.com/gabrielfalcao/lettuce获取源代码

  • 任何打算使用django生菜的用户应该知道默认情况下,它使用你的**默认**数据库进行测试.这个有趣的设计选择花了我一个生产数据库:( (7认同)
  • 生菜也有一些替代品,比如Behave; 这是[博客文章比较他们,并提倡行为](https://fortylines.com/blog/LettuceVsBehave.blog.html). (3认同)

Gre*_*ous 46

我真的推荐表现.

寻找Python的黄瓜克隆,我开始使用生菜,但发现它是一个非常笨拙设计的复制品.非常Unpythonic.

然后我发现了行为,并且对它非常满意.

  • 当我在django项目中使用默认数据库进行测试的默认行为花费了我在实时服务器上的生产数据库时,我切换到生菜行为:(我真的很喜欢行为;我已经启动了django-behave项目将其挂钩到django的测试框架https://github.com/rwillmer/django-behave (11认同)

Rya*_*yan 37

Ian Bicking建议将doctest用于行为驱动设计:

我个人倾向于在行为驱动的设计风格中使用鼻子空洞模拟.具体来说,鼻子的spec 插件非常适合BDD.

  • Andrew Bennetts最近写了几篇关于为什么他认为doctest被滥用的帖子.http://andrew.puzzling.org/diary/2008/October/23/narrative-tests http://andrew.puzzling.org/diary/2008/October/24/more-doctest-problems (7认同)
  • 我认为doctest实际上更符合BDD的理念,当你按照预期的方式对待它时:你开始写软件,然后用也形成测试的例子散布它.它也被描述为"文档驱动开发" - 重点是关注可向外描述的功能,而不是内部工作单元.我认为传统xUnit*在这方面很可怕. (4认同)

Dou*_*ata 29

我建议你使用一套工具来帮助程序员实践BDD和TDD.该工具集由:pycukes,specloud,ludibrioshould-dsl组成.

应该DSL会给你RSpec般的期望.您可以使用RSpec期望API执行的所有操作,ds-dsl也可以.你可以从Github获取最新版本.

SpecLoud可帮助您运行类似BDD的单元测试.你可以通过这样做来安装它

pip install specloud
Run Code Online (Sandbox Code Playgroud)

Ludibrio是一个测试双打(Mocks,Stubs和Dummies)的图书馆.通过安装它

pip install ludibrio
Run Code Online (Sandbox Code Playgroud)

PyCukes是BDD的主要工具.它将运行场景等.再次,

pip install pycukes
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请阅读PyPi上的工具文档.


Ste*_*eve 11

伟大的帖子和答案.只是想更新以包括Freshen在此列表中,因为我读取pycukes已停止.关于使用BDD和Django与Freshen的好帖子就在这里.


Gab*_*cão 9

您可以使用"确定"来表达断言(就像在RSpec中一样)


小智 8

Pyccuracy项目旨在为Python中的BDD提供特定于域的语言.

与在API级别工作的doctest不同,它对更高级别的操作进行编码,例如加载网页和提交表单.我没有使用它,但如果你正在寻找它,它看起来有点有希望.


Ref*_*ann 6

我非常喜欢Pyccuracy.这些天我正在中型项目上实施它.

  • 我对最近任何人都可以分享的Pyccuracy vs Lettuce比较感兴趣. (3认同)

mdw*_*ott 6

试试pyspecs.在开发过程中使测试易于阅读和持续运行是我创建此项目的两个主要目标.

测试代码:

from pyspecs import given, when, then, and_, the, this

with given.two_operands:
    a = 2
    b = 3

    with when.supplied_to_the_add_function:
        total = a + b

        with then.the_total_should_be_mathmatically_correct:
            the(total).should.equal(5)

        with and_.the_total_should_be_greater_than_either_operand:
            the(total).should.be_greater_than(a)
            the(total).should.be_greater_than(b)

    with when.supplied_to_the_subtract_function:
        difference = b - a

        with then.the_difference_should_be_mathmatically_correct:
            the(difference).should.equal(1)
Run Code Online (Sandbox Code Playgroud)

控制台输出:

# run_pyspecs.py

  | • given two operands 
  |   • when supplied to the add function 
  |     • then the total should be mathmatically correct 
  |     • and the total should be greater than either operand 
  |   • when supplied to the subtract function 
  |     • then the difference should be mathmatically correct 

(ok) 6 passed (6 steps, 1 scenarios in 0.0002 seconds)
Run Code Online (Sandbox Code Playgroud)