DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Manipulating Test Execution with ScalaTest

Jens Schauder user avatar by
Jens Schauder
·
Aug. 29, 11 · Interview
Like (0)
Save
Tweet
Share
6.36K Views

Join the DZone community and get the full member experience.

Join For Free

My favorite feature in the JUnit are Rules. With Rules you can manipulate the execution of your test, by wrapping your own code around the test execution. I have used this for setting up databases for test, making sure they execute in the EDT, limiting legal test execution time and for set up and tear down of various other resources.

So naturally when working with ScalaTest I pretty soon wanted to have something equivalent to Rules. While you find not much when searching for ‘Rules ScalaTest’, it is actually quite easy to implement.

No matter what kind of tests you write with ScalaTest, at the end of the day the method def withFixture(aTest : NoArgTest) will get called with your test to execute. ‘Your test’ means in case of a FunSuite test the stuff between the curly braces that you pass to the test method.

The implementation of withFixture will finally execute your test. So all you have to do in order to get the equivalent of a JUnit Rule is to override withFixture. Lets give it a try:

      class ExampleTest extends FunSuite {



override def withFixture(test : NoArgTest){

println

println("before")

super.withFixture(test)

println("after")

}



test("example test"){

println("succeding test")

}



test("example failing test"){

println("failing test")

fail("embrace failure")

}

}

This produces the following output on the console:

before
succeding test
after

before
failing test

As you can see, if you want to do stuff after the test, you better put it in a finally block, so it gets executed no matter if the test fails or not.

While this works, we don’t want to explicitly override withFixture every time we need our ‘rule’. The solution to this is to extract it into a trait:

  
trait ExampleRule extends AbstractSuite {

self : Suite =>

override abstract def withFixture(test : NoArgTest) {

println

println("before")

try {

ExampleRule.super.withFixture(test)

} finally {

println("after")

}

}

}



class Example2Test extends FunSuite with ExampleRule {

test("example test") {

println("succeding test")

}



test("example failing test") {

println("failing test")
fail("embrace failure")

}

}

The trait extends AbstractSuite, which makes withFixture available. It has a selftype of Suite, in order to ensure it gets only mixed into test suites.

So how does this solution compare to the implementation of a JUnit Rule? With a Junit Rule we have to create the actual Rule plus a Statement instance. With ScalaTest we only extend a single trait. Also since all this is standard Scala inheritance an no reflection involved, so it is pretty strait forward to find out how this works. While how and why a Rule gets executed is kind of tricky from the source alone. Finally you can explicitly control the order in which your traits apply, by controlling the order in which you put them in the extends clause:

      trait Example3Rule extends AbstractSuite {

self : Suite =>

override abstract def withFixture(test : NoArgTest) {

println("3 before")

try {

Example3Rule.super.withFixture(test)

} finally {

println("3 after")

}

}

}



@RunWith(classOf[JUnitRunner])

class Example3Test extends FunSuite with Example3Rule with ExampleRule{

test("example test") {
println("succeding test")

}

test("example failing test") {

println("failing test")

fail("embrace failure")

}

}

before
3 before
succeding test
3 after
after

before
3 before
failing test
3 after
after

With Junit Rules there is currently no ready to use way to do this, but a feature to achieve this is in development.

 

From http://blog.schauderhaft.de/2011/08/28/manipulating-test-execution-with-scalatest/

Testing Execution (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Container Security: Don't Let Your Guard Down
  • Introduction to Container Orchestration
  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Microservices Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: