是否有RSpec等效的RSpec来做TDD?

问题描述 投票:25回答:4

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

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

我还缺少另一种选择吗?或者我只是在滥用PyTest和Nose? Python社区是否采用了一些完全不同的方式来实现这一点,我应该停止尝试使它像Ruby一样?根据GitHub上的明星数量,社区似乎并没有将这些选项中的任何一个作为首选测试框架。

python ruby testing rspec tdd
4个回答
11
投票

我最接近做一个简短的谷歌搜索是mamba +期望:


9
投票

我爱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安装后,我能够运行自己的测试,并且工作正常!

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

    $ 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 ========================

3
投票

http://pythonhosted.org/behave/

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


2
投票

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

Testinfra旨在成为python中的Serverspec等价物,并作为插件写入强大的Pytest测试引擎

我宁愿做python,但我不得不处理ruby。这就是生活。

© www.soinside.com 2019 - 2024. All rights reserved.