简体   繁体   中英

How to get the context of the running tests in scala-test ? Is there anything like ITestContext as in TestNG to get those information?

I want to know the list of running test cases and manipulate with those information. In TestNG, implementing the onFinish, onStart, etc., methods of ITestListener gives ITestContext to retrieve those information. Is there anything similar to that in scala-test. Suggestions are highly appreciated. Thanks in advance.

Sky's answer is actually looking in the right direction. Mixing in ScalaTest's BeforeAndAfterAll trait gives you access to some contextual information about the suite, such as:

  • The suite name
  • The number of tests in the suite
  • The names of tests in the suite
  • Tag information for the suite

The information you get is perhaps not as rich as the contextual information you get from TestNG (for example, this trait won't be able to tell you which tests passed/failed in afterAll ). Maybe however the information it gives you is good enough for your purposes:

class MyTest extends FunSuite with BeforeAndAfterAll {

    override def beforeAll() {

        // suiteName will give you the name of the suite
        info(suiteName)

        // testNames will give you the names of your tests
        testNames forEach info(_)

        // tags will give you a mapping of test names to tags
        tags.keys.forEach(t => 
            info(t + " tagged with tags " + tags(t).mkString(",")))
    }

    ...
}

Yes,

Scalatest has BeforeAndAfter trait which has:

before{
//write code here(run before each test cases in a test file )
}

after{
// write code here(run after each test cases in a test file )
}

and an other trait BeforeAndAfterAll which has:

override def afterAll: Unit = {
  //write code here(run after all test cases in a test file )
}

override def beforeAll: Unit = {
//write code here(run before all test cases in a test file )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM