The Artima Developer Community
Sponsored Link

ScalaTest/ScalaUtils Forum
Scala FunSpec tests desribe() initialization and execution order

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Oleg Olefir

Posts: 1
Nickname: oleffir
Registered: Dec, 2017

Scala FunSpec tests desribe() initialization and execution order Posted: Dec 12, 2017 10:40 AM
Reply to this message Reply
Advertisement
I faced with a problem when try to use BDD approach with scalatest and mockito. To reduce code duplication I put each needed when() rule inside each of describe blocks. But I was surprised by the ordering how describe() blocks run.

class SomeTest extends FunSpec with BeforeAndAfterAll with MockitoSugar {

private val catalogClient = mock[CatalogServiceClient]

override def beforeAll {
when(catalogClient.getFrame(any)).thenReturn(Frame())
}

describe("MyTest1") {
println("Inside MyTest1")

when(catalogClient.getConnection(any))
.thenReturn(Conn(ID_FOR_TEST_1))

it("should perform action with data ID_FOR_TEST_1") {
println("Inside it 1")
}

it("should perform another action with data ID_FOR_TEST_1") {
///
}

}

describe("MyTest2") {
println("Inside MyTest2")

when(catalogClient.getConnection(any))
.thenReturn(Conn(ID_FOR_TEST_2))

it("should perform logic with data ID_FOR_TEST_2") {
println("Inside it 2")
}

it("should perform another logic with data ID_FOR_TEST_2") {
///
}
}
}

It printed:

"Inside MyTest1"
"Inside MyTest2"
"Inside it 1"
"Inside it 2"
while I was expecting

"Inside MyTest1"
"Inside it 1"
"Inside MyTest2"
"Inside it 2"

And the first test failed because mocked data replaced in the second describe() block.

So it firstly goes through all describe blocks and after that run the tests.

After some researching, I found path.FunSpec class that preserves ordering of each describe block, but it doesn’t allow use traits like BeforeAndAfter because of overriding runTest() method as final.

I would like to know some good practices to organise such tests with minimum code duplication. And some recommendations about my particular case.

Topic: Does Scalatest library supports Spark RDD/SC/DF? Previous Topic   Next Topic Topic: Quick Start Example Compilation Failure

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use