[TIP] Sclara is a Python testing DSL

Michael Foord michael at voidspace.org.uk
Mon Mar 26 13:42:53 PDT 2012


On 24/03/2012 21:00, John MacKenzie wrote:
> http://github.com/198d/sclara
>
> 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'):
>     assert True
>   with test('does something else'):
>     assert True
>   with description('when some condition is met'):
>     with test('acts a specific way'):
>       assert True
>
> 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).
>
> Thoughts?
>
> (I just realized I should start a blog or something...haha)


Interesting we had a discussion about a theoretical tool similar to this 
a while back. Here's my post with my suggested syntax (that I never got 
round to implementing - so kudos to you for doing it):

     http://permalink.gmane.org/gmane.comp.python.testing.general/3740

All the best,

Michael
>
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python


-- 
http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120326/d67f0e00/attachment-0001.htm>


More information about the testing-in-python mailing list