Cleanup/teardown in OneInstancePerTest / path.FunSpec

156 views
Skip to first unread message

John O'Malley

unread,
Mar 11, 2014, 10:52:41 AM3/11/14
to scalate...@googlegroups.com
I really prefer path.FunSpec to org.scalatest.FunSpec.  it's just more natural to have nested describes with  specific setup code embedded.

That being said, I don't see a convenient way to create a common cleanup/teardown block.  For example:

class MySpec extends path.FunSpec {
    val file = createAFile()

    describe("some tests") {
       it("does this") {
           // do something with the file
       }

       it("does something else") {
          // do something else with the file
       }
    }
}

Neither BeforeAndAfter nor BeforeAndAfterEach cannot be mixed in.  I can override "it" but that's pretty awkward.  Is there no better solution here?

Bill Venners

unread,
Mar 11, 2014, 12:19:22 PM3/11/14
to scalate...@googlegroups.com
Hi John,

Are you wanting to use the same file in both tests, and observing that path.FunSpec will create a new file for each test?

Bill


--
You received this message because you are subscribed to the Google
Groups "scalatest-users" group.
To post to this group, send email to scalate...@googlegroups.com
To unsubscribe from this group, send email to
scalatest-use...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/scalatest-users?hl=en
ScalaTest itself, and documentation, is available here:
http://www.artima.com/scalatest
---
You received this message because you are subscribed to the Google Groups "scalatest-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalatest-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Bill Venners
Artima, Inc.
http://www.artima.com

John O'Malley

unread,
Mar 11, 2014, 12:57:32 PM3/11/14
to scalate...@googlegroups.com
I want to create a file before each test and delete it afterwards.  If the tests ever need to run in parallel I could use a guid or some other mechanism to generate the file name if need be - but I'm not running tests in parallel now (at least i don't think i am).

The only way to make it work seems to be:

override val it  = new ItWord {
  override def apply(...) = try {
    super.apply(...)
  } finally doCleanup()    
}

It would seem to me that some sort of afterEach hook might be useful.  

Bill Venners

unread,
Mar 11, 2014, 1:21:07 PM3/11/14
to scalate...@googlegroups.com
Hi John,

Oh, well in a path trait you just create the file textually before the test and clean it up textually after. Before I show an example, the default in ScalaTest is that tests are *not* run in parallel. To get tests in the same suite class to run in parallel, you need to mix in ParallelTestExecution. This is not allowed in a path trait because it would be very hard for people to reason about. What you can do, though, is run different test classes in parallel (just not the tests inside the same class). sbt does this by default. ScalaTest's Runner requires a -P command line arg.

I would do:

class MySpec extends path.FunSpec {
    val file = createAFile()
    try {

      describe("some tests") {
         it("does this") {
             // do something with the file
         }

         it("does something else") {
            // do something else with the file
         }
      }
    }
    finally file.close()
}

Path traits are so intuitive that they are non-intuitive. That will really work. The reason is that each test runs in its own instance, and all the code leading up to the test (the path) will get re-executed for each test (and nothing not on the path will execute). Thus each will get a different instance of the file and close it after it is done. No need to worry about making the file name unique because tests in the same class won't run in parallel, but tests in different classes can run in parallel with each other.  So if you need to use this technique in multiple test classes, each test class should have its own file name.

Please give that a shot and let me know if you have any trouble.

Bill
Reply all
Reply to author
Forward
0 new messages