How can I implement BeforeAfterAll myself for 2.3.9?

29 views
Skip to first unread message

Sam Lee

unread,
Jun 10, 2015, 3:13:22 PM6/10/15
to specs2...@googlegroups.com
Hello,

I am locked with specs2 version 2.3.
I want to set up a database connection before all examples. And disconnect after all examples.
DB setup and teardown code should run only once during `sbt test`, not on each test or test suite. 
Every test that uses shared db connection should run in sequence.

How can I implement these requirements, in 2.3?
This version does not have BeforeAfterAll trait, which seems to be useful to me.

How can I implement BeforeAfterAll using 2.3 API ?

Thanks.

Sam Lee

unread,
Jun 10, 2015, 3:38:21 PM6/10/15
to specs2...@googlegroups.com
Actually, what I want is to group Specifications together and execute fragments in a single context.
For example, given:

class FooSpec extends BaseSpec ....
class BarSpec extends BaseSpec ...
trait BaseSpec extends Specification {
   override def map(fragments: => Fragments) = Step(initialize) ^ fragments ^ Step(teardown)
   def initialize ...
   def teardown ...
}


I want fragments in FooSpec and BarSpec be combined  and run sequentially after single invocation of initialize Step followed by single invocation of teardown Step.

How can I accomplish this?



--
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.

etorreborre

unread,
Jun 10, 2015, 8:37:57 PM6/10/15
to specs2...@googlegroups.com, sky...@gmail.com
You can do the following:

class FooBarSpec extends Specification with BaseSpec { def is = s2"""
  Foo spec
  ${removeSetupTeardown(new FooSpec)}

  Bar spec
  ${removeSetupTeardown(new BarSpec)}
"""
  
  // this function will have to be implemented differently in 3.x 
  def removeSetupTeardown(spec: Specification): Fragments = 
    spec.fragments.middleDrop(1).middleDropRight(1)
}

Eric.
To unsubscribe from this group and stop receiving emails from it, send an email to specs2-users+unsubscribe@googlegroups.com.

Sam Lee

unread,
Jun 11, 2015, 12:23:48 PM6/11/15
to specs2...@googlegroups.com
What is scala way of writing s2?

I tried,

class FooBarSpec extends Specification with BaseSpec {
  removeSetupTeardown(new FooSpec())
  removeSetupTeardown(new BarSpec())

  private def removeSetupTeardown(spec: Specification) = spec.fragments.middleDrop(1).middleDropRight(1)
}

But this gives me compiler error:
missing arguments for method fragments in trait FragmentsBuilder;
[error] follow this method with `_' if you want to treat it as a partially applied function
[error]     spec.fragments.middleDrop(1).middleDropRight(1)

Possibly because FooSpec and BarSpec are mutable.Specificaitons ?


So, I tried a different way: 
FooSpec and BarSpec do not extend BaseSpec anymore, but have skipAllUnless(dbConnection)  argument.

class FooBarSpec extends BaseSpec {
  sequential

  new FooSpec() 
  new BarSpec()
}

Result: Compiles successfully. FooSpec and BarSpec are skipped. Then it enters FooBarSpec.
But FooSpec inside FooBarSpec complains that there's no db connection.
BaseSpec wraps fragments with initialization and teardown... Not sure why this is happening.


Also tried,

class FooBarSpec extends Specification {
  sequential

  Step(DB.initialize)
  new FooSpec()
  new BarSpec()
  Step(DB.teardown)
}

But above does not compile because missing `is` method definition.






}

Eric.
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.

--
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.

etorreborre

unread,
Jun 11, 2015, 9:49:09 PM6/11/15
to specs2...@googlegroups.com, sky...@gmail.com
You can try this:


trait BaseSpec extends org.specs2.mutable.Specification {
  override def map(fragments: => Fragments) = Step(initialize) ^ fragments ^ Step(teardown)
  def initialize = println("init")
  def teardown = println("teardown")
}
class FooSpec extends BaseSpec {
  "foo" >> { println("foo"); ok }
}

class BarSpec extends BaseSpec {
  "bar" >> { println("bar"); ok }
}

class FooBarSpec extends org.specs2.mutable.Specification with BaseSpec {
  override def is = br ^
    removeSetupTeardown(new FooSpec()) ^
    removeSetupTeardown(new BarSpec())

  private def removeSetupTeardown(spec: org.specs2.mutable.Specification) =
    spec.name ^ br ^
    spec.is.middleDrop(1).middleDropRight(1) ^ br ^ br ^ end
}

Output:

init
bar
foo
teardown
[info] FooBarSpec
[info] FooSpec
[info] + foo
[info]
[info] BarSpec
[info] + bar

Eric.
}

Eric.
To unsubscribe from this group and stop receiving emails from it, send an email to specs2-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages