Tests as first-class values

8 views
Skip to first unread message

Mauricio Scheffer

unread,
Dec 15, 2014, 5:24:07 AM12/15/14
to specs2...@googlegroups.com
Hi, a few days ago I asked on twitter about test libraries that represent tests as first-class values. 
Eric replied that specs2 can do that ( https://twitter.com/etorreborre/status/543130320380231680 ), but I can't seem to find any examples of it. Can anyone point me to any such examples?
Concretely, I want to be able to run higher-order functions on test functions, alter test names, etc. and then when I'm done building the test tree, run it.

Thanks!
Mauricio

etorreborre

unread,
Dec 15, 2014, 5:50:21 AM12/15/14
to specs2...@googlegroups.com
Hi Mauricio,

specs2 is a bit different than other libraries. First of all there is no "test tree" but a list of "Specification fragments".

They are of different type:

 - Text
 - Example
 - Step
 - "Formatting fragments": br, p, end, t
 - ...

And basically when you org.specs2.Specification or org.specs2.mutable.Specification you inherit 2 DSLs to create those fragments:

 - in org.specs2.Specification you use an "interpolated string" to create them so that your specification looks like an annotated text

 - in org.specs2.mutable.Specification you use String extension methods, should / in, to create the fragments

You can however directly create your specification by creating those fragments yourself:

class MySpec extends Specification { def is = 
  Text("hello") ^ br ^
  Example("this should work", ok)
}

This means that you can use this low-level api to create whatever Fragments you need, assemble them with the ^ operator, and use them to define the Specification.is method.

A word of caution though, this API is going to evolve with specs2 3.0 (available as 3.0-M2 right now but I'm still working on finishing a few things + the migration guide) around 3 axes:

 - only one Fragment type, composed of a Description and an Execution (this covers all previous cases of Text, Example,... and more)

 - some "factory" traits to create fragments, with methods like: text, example, step,...

 - some DSLs traits to add syntactic sugar (for the interpolated s2 string, should / in, ...)

Perhaps you could present an example of what you wish to achieve and I can show you how to do it.

Eric.

Mauricio Scheffer

unread,
Dec 16, 2014, 6:45:50 AM12/16/14
to specs2...@googlegroups.com
Thanks Eric. There's a lot to digest here, as I'm new to Specs2. It looks like a very different composition model. The future Description + Execution model you mention does sound a lot simpler though, so I'm looking forward to that.

Whenever I start working in a new platform I look for test libraries with tests as first-class values. In my experience, when there isn't a clear composition model, you eventually start working around the test library or digging into its internals instead of writing simple functions that manipulate tests just like any other value.

I'll try to keep to the regular usage at first while I learn more about Specs2.

Cheers

--
Mauricio

--
You received this message because you are subscribed to the Google Groups "specs2-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to specs2-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages