scalatest needless evaluation of test body of ignored tests

12 views
Skip to first unread message

Hadar Rottenberg

unread,
May 8, 2016, 8:51:25 AM5/8/16
to scalatest-users
I wrote a few tests like

"x" should {
 val myMultipleTestsObject = initX
 "validate x" in {    
 }
"validate x 2" in {
 }
}

"y" should {
 val myMultipleTestsObject = initY
 "validate y" in {    
 }
"validate y 2" in {
 }
}


I'm trying to run for example only the "y" test, and i found out that the body of "x" is also evaluated when it's being registered and thus it excutes "initX"
for me initX and initY are long running tasks that I would like to avoid when running only one test.
I solved this by adding lazy to myMultipleTestsObject.
but is this the expected behaviour? isnt there a more idiomatic way to prepare variables for each test without them being evaluated on registration time?



Bill Venners

unread,
May 8, 2016, 11:37:27 AM5/8/16
to scalate...@googlegroups.com
Hi Hadar,

That is by design, so that we can know how many tests are coming before we execute the suite. This helps make a nice progress bar as your suite runs. You'll see it in IntelliJ, for example.

Nothing wrong with lazy. It is the intended way to achieve what you want in a traditional ScalaTest style. One other style you may want to look at is org.scalatest.path.FunSpec and FreeSpec. They only execute setup code on the path to each executed test. That would be another way to get the behavior you're after.

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

Hadar Rottenberg

unread,
May 9, 2016, 6:09:29 AM5/9/16
to scalatest-users
Thanks for the quick reply,
Both FreeSpec and FunSpec behave the same as WordSpec,
they all evaluate what's on the path before the test "in { }" clause.
why do you need to evaluate the test body in order to register it? cant it just store the method?
Reply all
Reply to author
Forward
0 new messages