40

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.

williamcodes
  • 6,317
  • 8
  • 32
  • 55
  • Have you searched for `RSpec for Python`? – Peter Wood May 19 '16 at 21:59
  • 24
    Yes, and was SHOCKED there wasn't already a stackoverflow post for that, hence me posting this. – williamcodes May 19 '16 at 22:05
  • I love python but I too wish I could have the magic of rspec. Part of what makes rspec so amazing is Ruby which makes super fluent DSLs possible. Most Python libraries that I've seen use a unit testing approach so the tools tend to be more mature for that. Anyway, here's a post with a bunch of suggestions http://stackoverflow.com/questions/231371/practicing-bdd-with-python – Marshall May 19 '16 at 22:40
  • 1
    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 May 20 '16 at 06:55
  • 1
    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 Aug 06 '16 at 23:46
  • 1
    @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 Oct 01 '21 at 07:16
  • @HectorOrdonez This conversation is over 5 years old. – Peter Wood Oct 01 '21 at 20:58
  • There is a new plugin called pyspec. – felipecrp May 04 '23 at 02:17

4 Answers4

22

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

Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
Torben Knerr
  • 784
  • 6
  • 15
14

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 ========================
AnneTheAgile
  • 9,932
  • 6
  • 52
  • 48
  • 1
    +1, also using pytest + spec + testinfra. I wanted rspec like definition of specs with nested contexts etc initially, but then I noticed that the spec formatted output is actually more important than the rspec / context style of writing tests. Happy with pytest + spec formatter for now :) – Torben Knerr Aug 24 '16 at 12:25
  • @Torben-knerr checkout pytest-pspec. – gwthm.in Jan 23 '18 at 03:16
  • 1
    @7H3IN5ID3R pytest-pspec looks nice! – Torben Knerr Feb 02 '18 at 12:15
  • This library looks nice and solves the problem, but it appears to be a little ill-maintained at this point. Sparse commits from years ago, and the issues and PRs are out of date. It's tough to tell about adoption, because pypi.org doesn't publish download or dependency stats per library. It might be just fine if there are lots of projects out there using it in the wild. – kdbanman Oct 30 '18 at 23:41
  • This looks really nice! But it is GPL licensed so not suitable in my case -- I don't think my employer has the plan to open-source our code ... – Yun Wu Apr 29 '22 at 10:04
5

http://pythonhosted.org/behave/

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

Ravi Sankar Raju
  • 2,850
  • 3
  • 18
  • 16
1

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.

  • 1.On py2.7 with conda, I got this to work to test a file exists, but not package nor url pingable. I'm on OSX 10.11, so perhaps on linux it would work - that's what it seems from the code.; 2.I could not install with pip, I had to use the source option; pip install -e git+git://github.com/jaimegildesagredo/server-expects.git#egg=server-expects – AnneTheAgile Aug 06 '16 at 23:34