Doing something before or after all Scalatest tests

后端 未结 3 1356
情歌与酒
情歌与酒 2020-11-29 01:32

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

3条回答
  •  甜味超标
    2020-11-29 01:56

    Ok, found a way. It seems (unless someone here can correct me) that Scalatest does not have the facility of a "master" suite. But... you can kinda build one.

    You can compose a suite from traits. So using my endpoint example:

    class EndpointTests extends FunSpec with MustMatchers with BeforeAndAfterAll 
          with Foo with Bar {
            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
            }   
    }
    

    Ok, but what about the tests? Notice the with Foo with Bar. I'm bringing the dependent tests in as traits. See here:

    trait Foo extends FunSpec with MustMatchers {
        describe("Message here...") {
            it("Must do something") {  }
            it("Must be ok") {  }
        }
    }
    
    trait Bar extends FunSpec with MustMatchers { 
        describe("Hello you...") {
            it("One more!") {  }
        }
    }
    

提交回复
热议问题