36
votes

I'm looking for a test framework like Ruby's RSpec to do test driven development in Python. The advantage of a framework like RSpec is that it offers a DSL that lends itself well to TDD. First you describe the test in english, and then you write the test, and when it fails you get a message saying what test failed with your nice description of what the test is trying to do.

So far I've looked at PyTest and Nose. PyTest seems closer to ruby's MiniTest than RSpec. Instead of offering a DSL with language to make it read like specifications, it focuses on assertions. Nose seems like a wrapper on PyTest that doesn't add its own DSL.

Is there another option I'm missing? Or am I just misusing PyTest and Nose? Has the Python community settled on some totally different way of doing this and I should stop trying to make it like Ruby? It doesn't seem, based on the number of stars on GitHub, that the community has really anointed either of these options as the preferred testing framework.

4
Have you searched for RSpec for Python?Peter Wood
Yes, and was SHOCKED there wasn't already a stackoverflow post for that, hence me posting this.williamcodes
The reason there aren't many Stack Overflow posts is because it is off topic to ask for tool recommendations. The first page of search results I got had lots of useful ideas, but you don't mention any of them. The post @Marshall linked is third in the list, and is also closed as being off topic but does have useful suggestions, again none of which you mention.Peter Wood
I love this topic! I have done google many many times and not found what I wanted. Finally this was it all in a few words. BDD, the closed post from 2008 you referenced, is different from the Rspec format. Being from 2008 it also has primarily obsolete references.AnneTheAgile
@PeterWood Isn't it ironic that you ask if he already searched, when you could have searched yourself if he already searched by searching yourself?Hector Ordonez

4 Answers

20
votes

The closest I have come to doing a brief google search was mamba + expects:

14
votes

I love Rspec! On python, I am going to use a py.test plugin called spec: https://pypi.python.org/pypi/pytest-spec https://github.com/pchomik/pytest-spec

It uses unittest, the default python package, plus pytest and itself. Upon cloning the project to my python 2.7 conda OSX 10.11 installation, I was able to run its own tests, and it worked fine!

The format is simple, but it includes the basics: a group name, the pass/fail/skip status, and the name of the test spelled out with spaces instead of underscores. Here's some output from their own tests, which seem simple for me to follow on my own.

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

http://pythonhosted.org/behave/

This is one solution to behavior driven development in python. Might help.

1
votes

There is also https://testinfra.readthedocs.io/en/latest/ if you can use servespec which according to the website says

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

I'd much rather be doing python but I'm having to deal with ruby. C'est La Vie.