Re: [Cedar] Running Specs in isolation

53 views
Skip to first unread message

sco...@pivotallabs.com

unread,
Nov 22, 2012, 11:34:39 AM11/22/12
to cedar-...@googlegroups.com, cedar-...@googlegroups.com
Where is this setup code?  Ideally you should only have test setup in beforeEach() blocks contained within the relevant describe()/context().  These beforeEach() blocks should only run for those tests contained within the same describe/context block.

You may have setup code in a +beforeEach method.  If so, this is global and your only option is to comment the setup code out.  In an ideal world you would only have code that cleans up test pollution in these methods.

If you want to limit which specs run, you can prefix it/describe/context blocks with the letter f (fit/fdescribe/fcontext).  When you do this Cedar will only run those specs.

You can also take a look at Dmitriy Kalinin's excellent Cedar Shortcuts [1] plugin which will let run isolated tests via shortcut key instead of having to prefix blocks with 'f'.


On Nov 22, 2012, at 12:42 AM, adam....@ennova.com.au wrote:

I have a number of different Cedar specs that have some setup code before the specs. Each SPEC_BEGIN / END are in different files but my setup code from each of the files seems to be run before any of my specs. Is there a way I can run each file in complete isolation?

Thanks

Adam Milligan

unread,
Nov 22, 2012, 8:28:05 PM11/22/12
to cedar-...@googlegroups.com
I understand what's happening.  Code inside a describe block is executed at the time the specs are defined, not run; after all, the example definitions themselves are simply executable code.  This means that any code executed in a describe block will run once, and only once.  This can be helpful for setting up immutable state, but keep in mind it can lead to test pollution since the setup is not refreshed for each example.

As far as running a setup for a specific file, unfortunately there's no way to do that.  Cedar has no concept of files, only of examples and example groups.  From an implementation point of view, Cedar instantiates each spec class (delineated by SPEC_BEGIN and SPEC_END) at runtime and executes the example definition code; it doesn't actually execute any examples until after all example definition code has been run.  This is because examples in file A can depend on shared example groups in file B, or on beforeEach blocks later in the same file.  

If your setup time is long enough that using a beforeEach is impractical, then you could set up separate spec targets for each long-running setup.  This is a bit inconvenient for running everything, but it should be a simple task to write a rake task or bash script to run them all in sequence.

On Thu, Nov 22, 2012 at 6:28 PM, <adam....@ennova.com.au> wrote:
Currently I just have the code inside a describe block, I'm trying to avoid a beforeEach() block as the setup is relatively slow but maybe thats what I have to do. The problem occurs when setup code from file B is run before specs in file A. 

Is there a way I can create a file context type thing or run each separate spec file in isolation so that setup code in file A is run, then specs in file A, followed by the setup in file B and the specs in file B?
Reply all
Reply to author
Forward
0 new messages