How to group tests using specs2?

烂漫一生 提交于 2019-12-05 04:23:39
Eric

In specs2 there is no concept of a hierarchical suite. A Specification is just a list of examples. Even when you group them with xxx should yyy, this just affects the way the examples are displayed in the console, with more or less indentation.

On the other hand there are ways to organize specifications with specs2:

References

You can create a hierarchy of specifications by creating a top-level specification referencing other ones:

// code for specs2 3.x
class ParentSpec extends Specification { def is = s2"""
  These are all the important specifications about our domain
  ${"child1" ~ ChildSpec1}   
  ${"child2" ~ ChildSpec2}   
  """
}

The child specifications can reference other specifications and so on. What's different from JUnit (and possibly from ScalaTest) is that your reference graph doesn't need to be a tree. When you execute a Specification with the all argument

sbt> test-only ParentSpec -- all

then dependencies are followed from the ParentSpec so that the low-level dependencies are executed before the high-level ones. And any cycles are broken so that you won't be executing things infinitely (or get a StackOverflowError).

Tags

Tags are a very convenient way to classify things because a given "thing" doesn't need to belong to only one category. This was, at the time, one of the great improvements brought by TestNG. In specs2 you can tag single examples or whole specifications and then declare which examples you want to run based on the inclusion/exclusion of some tags. For example

class Spec1 extends mutable.Specification { section("functional")
   "simple test" >> ok

   tag("io")
   "a bit of IO" >> ok
}

class Spec2 extends mutable.Specification { section("functional")
   "another simple test" >> ok

   tag("io")
   "another bit of IO" >> ok
}

Then you can execute only the specifications with the functional tag but not with the examples having the io tag

sbt> test-only -- include functional exclude io

Organization

Using references and tags you can probably imagine several ways to slice and dice your test source:

  • you can use references to create a main "taxonomy" of specifications
  • you can use tags to create "cross-cutting" concerns like io, slow, database, scalacheck...

Note that you can also mix all of the above and have tags on your references, specifications with both examples and references, and so on.

The criteria for choosing a given structure are:

  • the navigation around concepts in the code base
  • the execution speed of different suites
  • the necessity to re-run only some aspects of your tests after a change
  • the infrastructure constraints (not everything can run in any environment)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!