[TIP] Sclara is a Python testing DSL
john at nineteeneightd.com
Sat Mar 24 14:00:06 PDT 2012
I wrote this library based on a thought I had at the TiP BoF this year
(hence the name "s(anta)clara"): RSpec for Python (unoriginal idea) written
entirely with context managers (original idea, hopefully). I had been
writing Ruby for 3 years up until about December of last year and have been
writing Python full time since then. What I like most about RSpec is the
way you define and organize tests. You end up grouping similar test cases
in a sort of hierarchical manner (without resorting to OOP, which I hate
the most about writing tests with unittest) and get things like cumulative
setup/teardown from parent contexts while producing an english statement
that is either true or false after the test is run. These are the main
ideas I tried to capture with sclara. Context mangers, at first glance kind
of make sense; if you make a quick sketch of what this might look like, you
get something like this:
with description('Our object under test'):
with test('does something'):
with test('does something else'):
with description('when some condition is met'):
with test('acts a specific way'):
Production test cases that read:
Our object under test does something.
Our object under test does something else.
Our object when some condition is met acts a specific way.
It gets a bit more complicated once you remember how context managers
actually work (e.g. delayed execution). My answers to a lot of the
questions that will arise when you start thinking about how you'd actually
implement this are greenlets and AST manipulation. Check out sclara.runner
and sclara.builder for the more interesting parts. You can run the examples
like so (from the root of the project):
$ PYTHONPATH=. python examples/test_sclara.py
$ PYTHONPATH=. python sclara/plugin.py examples/test_simple.py
There are failures in both as a demonstration of how this interacts with a
TestRunner. The first one proves mainly that this is just Python; if you
use one of the provided runners you can execute your test files with the
Python interpreter (a big goal after looking at a tool like komira for this
sort of thing). The latter demonstrates that a nose plugin is possible and
probably implies that a py.test plugin is doable to.
Beyond sclara, I'm considering the idea that, more than a testing
framework, I stumbled on an interesting pattern for building generic DSLs
in Python (more on this soon, hopefully).
(I just realized I should start a blog or something...haha)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the testing-in-python