seratch / scalatest

Automatically exported from code.google.com/p/scalatest
Apache License 2.0
0 stars 0 forks source link

With WordSpec and OneInstancePerTest, all code inside "should" and "when" blocks that are not in scope are executed #40

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Using Scala 2.9.2 and ScalaTest 1.9.1, when using OneInstancePerTest, I would 
expect the test to only execute the code inside the "should" and "when" blocks 
for the current test. This is not the case as demonstrated with the following 
reproduction:

class OneInstancePerTestExecutesOtherScopes extends WordSpec with 
ShouldMatchers with OneInstancePerTest {
  var i = 0
  "Foo" should {
    i += 8
    "Not in scope" should {
      i += 4
      "Not in scope" when {
        i += 2
        "Another test" in {
          i += 1
        }
      }
    }

    "In scope" should {
      i += 4
      "In scope" when {
        i += 2
        "Actual test running" in {
          i should be (20) // Should be 14
        }
      }
    }
  }
}

Original issue reported on code.google.com by richardw...@googlemail.com on 14 Jun 2013 at 8:57

GoogleCodeExporter commented 8 years ago
That's actually kind of a feature. What happens is that at construction time, 
the only code that isn't actually executed is the body of the tests themselves 
which are demarcated by:

"do some behavior" in { ... }

Everything surrounding that is executed at construction time so that all tests 
can be registered at construction time. This enables the total number of 
expected tests to be known prior to run being invoked, which in turn means a 
GUI can put up a nice progress bar (we know how many tests are expected, so we 
can tell how far we got). If there are any side effects at the top level, or in 
a scope ("should" or "when" clauses) in a WordSpec, you'll see them at 
construction time. If you mix in OneInstancePerTest, you'll see those side 
effects once per test.

Best practice is to move the side effects into something like a withFixture or 
before/after, etc. Another thing to look at is org.scalatest.path.FreeSpec or 
org.scalatest.path.FunSpec. (There's no path.WordSpec, because the words are in 
the wrong places English-wise.) The path traits do execute just the path to the 
test each time, for the purpose of eliminating unnecessary side effects that 
are not in the "path" to the test.

Original comment by b...@artima.com on 14 Jun 2013 at 12:31

GoogleCodeExporter commented 8 years ago
That you for clarifying this. I will experiment with the path specs to see if 
they fit my needs. Feel free to close this issue.

Original comment by richardw...@googlemail.com on 19 Jun 2013 at 11:54