Sarath
Sarath

Reputation: 1496

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.

Upvotes: 0

Views: 726

Answers (2)

Sky
Sky

Reputation: 2609

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 )
}

Upvotes: 0

avik
avik

Reputation: 2708

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(",")))
    }

    ...
}

Upvotes: 1

Related Questions