Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing something before or after all Scalatest tests

I have a suite of scalatest tests that test different endpoints of a RESTful API. I really want them separated into different files for best organization.

My problem is how to start something (an HTTP server in my case, but it doesn't matter what it is) before all the tests and shut it down after all the tests are done.

I know about BeforeAndAfterAll, but that only accomplishes before/after inside one test file. I need something like that, but for all tests, for example:

-- start http server before tests
-- run all test suites
-- shut down http server

like image 494
Greg Avatar asked Mar 15 '13 01:03

Greg


People also ask

How do you ignore a test in ScalaTest?

It has been inserted into Scaladoc by pretending it is a trait. When you mark a test class with a tag annotation, ScalaTest will mark each test defined in that class with that tag. Thus, marking the SetSpec in the above example with the @Ignore tag annotation means that both tests in the class will be ignored.

What is a fixture in ScalaTest?

Regarding to the ScalaTest documentation: A test fixture is composed of the objects and other artifacts (files, sockets, database connections, etc.) tests use to do their work. When multiple tests need to work with the same fixtures, it is important to try and avoid duplicating the fixture code across those tests.

What is ScalaTest SBT?

ScalaTest is one of the main testing libraries for Scala projects, and in this lesson you'll see how to create a Scala project that uses ScalaTest. You'll also be able to compile, test, and run the project with sbt.


1 Answers

The intended way to do this is to use nested suites. Suite has a nestedSuites method that returns an IndexedSeq[Suite] (in 2.0, in 1.9.1 it was a List[Suite]). Suite also has a runNestedSuites method that is responsible for executing any nested suites. By default runNestedSuites calls nestedSuites, and on each returned Suite either invokes run directly, or if a Distributor is passed, puts the nested suites in the distributor so that they can be run in parallel.

So what you really probably want to do is make Foo and Bar into classes, and return instances of them from the nestedSuites method of EndpointTests. There's a class that makes that easy called Suites. Here's an example of its use:

import org.scalatest._ import matchers.MustMatchers  class Foo extends FunSpec with MustMatchers {   describe("Message here...") {     it("Must do something") {  }     it("Must be ok") {  }   } }  class Bar extends FunSpec with MustMatchers {   describe("Hello you...") {     it("One more!") {  }   } }  class EndpointTests extends Suites(new Foo, new Bar) with BeforeAndAfterAll {    override def beforeAll(configMap: Map[String, Any]) {     println("Before!")  // start up your web server or whatever   }         override def afterAll(configMap: Map[String, Any]) {     println("After!")  // shut down the web server   }          } 

One potential problem, though, is that if you are using discovery to find Suites to run, all three of EndpointTests, Foo, and Bar will be discovered. In ScalaTest 2.0 you can annotate Foo and Bar with @DoNotDiscover, and ScalaTest's Runner will not discover them. But sbt still will. We are currently enhancing sbt so that it passes over otherwise discoverable Suites that are annotated with DoNotDiscover, but this will be in sbt 0.13, which isn't out yet. In the meantime you can get sbt to ignore them by adding an unused constructor parameter to Foo and Bar.

like image 157
Bill Venners Avatar answered Oct 04 '22 14:10

Bill Venners