是否有 python 等效于 RSpec 来执行 TDD?

Is there a python equivalent for RSpec to do TDD?

我正在寻找像 Ruby 的 RSpec 这样的测试框架来在 Python 中进行测试驱动开发。像 RSpec 这样的框架的优势在于它提供了适合 TDD 的 DSL。首先,您用英语描述测试,然后编写测试,当它失败时,您会收到一条消息,说明什么测试失败了,并附有您对测试尝试进行的很好的描述。

到目前为止,我已经了解了 PyTest 和 Nose。 PyTest 似乎比 RSpec 更接近 ruby 的 MiniTest。它没有提供带有语言的 DSL 以使其读起来像规范,而是专注于断言。 Nose 看起来像是 PyTest 上的一个包装器,它没有添加自己的 DSL。

我还缺少其他选项吗?或者我只是在滥用 PyTest 和 Nose? Python 社区是否已经确定了一些完全不同的方式来做这件事,我应该停止尝试让它像 Ruby 那样?根据 GitHub 上的星数,社区似乎并没有真正指定这些选项中的任何一个作为首选测试框架。

http://pythonhosted.org/behave/

这是 python 中行为驱动开发的一种解决方案。可能有帮助。

还有 https://testinfra.readthedocs.io/en/latest/ 如果你可以使用 servespec 根据网站说

Testinfra aims to be a Serverspec equivalent in python and is written as a plugin to the powerful Pytest test engine

我更愿意做 python,但我不得不处理 ruby。 C'est La Vie.

我最接近进行简短 google 搜索的是 mamba + expects:

我爱Rspec!在 python,我将使用一个名为 spec 的 py.test 插件:https://pypi.python.org/pypi/pytest-spec https://github.com/pchomik/pytest-spec

它使用 unittest,默认 python 包,加上 pytest 和它自己。将项目克隆到我的 python 2.7 conda OSX 10.11 安装后,我能够 运行 它自己的测试,并且运行良好!

格式很简单,但它包括基础知识:组名、pass/fail/skip 状态以及用空格而不是下划线拼写的测试名称。这是他们自己的测试的一些输出,这对我来说似乎很简单。

    $ py.test --spec
================================ test session starts =================================
platform darwin -- Python 2.7.11, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/ME/src/pytestspec, inifile: setup.cfg
plugins: spec-1.0.1, testinfra-1.4.1
collected 30 items 

test/test_patch.py::TestPatch
    [PASS]  Pytest runtest logreport honors capitalization of words in test name
    [PASS]  Pytest runtest logreport marks method marked by double underscores
    [PASS]  Pytest runtest logreport prints class name before first test result
    [PASS]  Pytest runtest logreport prints test name and failed status
    [PASS]  Pytest runtest logreport prints test name and handle only single marker
    [PASS]  Pytest runtest logreport prints test name and passed status
    [PASS]  Pytest runtest logreport prints test name and skipped status
    [PASS]  Pytest runtest logreport returns none when letter is missing
    [PASS]  Pytest runtest logreport returns none when nodeid is wrong formatted
    [PASS]  Pytest runtest logreport returns none when word is missing
    [PASS]  Pytest runtest logreport skips empty line for first test
    [PASS]  Pytest runtest logstart returns none

test/test_plugin.py::TestPlugin
    [PASS]  Pytest adoption adds spec option
    [PASS]  Pytest adoption gets general group
    [PASS]  Pytest configure reloads pytest after patching
    [PASS]  Pytest configure should not reload configuration

test/test_replacer.py::TestPatcher
    [PASS]  Logstart replacer returns result of pytest runtest logstart method
    [PASS]  Report replacer returns result of pytest runtest logreport method

test/test_formats/test_functions.py
    [PASS]  Some function returns none
    [PASS]  Some function single underscore as prefix
    [PASS]  Some function single underscore as suffix

test/test_formats/test_methods.py::TestFormats
    [PASS]  Some method returns none
    [PASS]  Some method single underscore as suffix
    [PASS]  Some method single underscore as prefix

test/test_results/test_as_class.py::TestResults
    [SKIP]  Some method return none
    [SKIP]  Some method returns false
    [PASS]  Some method returns true

test/test_results/test_as_functions.py
    [PASS]  Some method returns true
    [SKIP]  Some method returns false
    [SKIP]  Some method return none

======================== 26 passed, 4 skipped in 0.14 seconds ========================