Can I have dependencies between scalaTest specs such that if a test fails, all tests dependent on it are skipped?
I didn't add that feature of TestNG because I didn't at the time have any compelling use cases to justify it. I have since collected some use cases, and am adding a feature to the next version of ScalaTest to address it. But it won't be dependent tests, just a way to "cancel" a test based on an unmet precondition.
In the meantime what you can do is simply use Scala if statements to only register tests if the condition is met, or to register them as ignored if you prefer to see it output. If you are using Spec, it would look something like:
if (databaseIsAvailable) {
it("should do something that requires the database") {
// ...
}
it ("should do something else that requires the database") {
}
}
This will only work if the condition will be met for sure at test construction time. If the database for example is supposed to be started up by a beforeAll method, perhaps, then you'd need to do the check inside each test. And in that case you could say it is pending. Something like:
it("should do something that requires the database") {
if (!databaseIsAvailable) pending
// ...
}
it("should do something else that requires the database") {
if (!databaseIsAvailable) pending
// ...
}
Here is a Scala trait that makes all test in the test suite fail, if any test fails.
(Thanks for the suggestion, Jens Schauder (who posted another answer to this question).)
Pros: Simple-to-understand test dependencies.
Cons: Not very customizable.
I use it for my automatic browser tests. If something fails, then usually there's no point in continuing interacting with the GUI since it's in a "messed up" state.
License: Public domain (Creative Common's CC0), or (at your option) the MIT license.
import org.scalatest.{Suite, SuiteMixin}
import scala.util.control.NonFatal
/**
* If one test fails, then this traits cancels all remaining tests.
*/
trait CancelAllOnFirstFailure extends SuiteMixin {
self: Suite =>
private var anyFailure = false
abstract override def withFixture(test: NoArgTest) {
if (anyFailure) {
cancel
}
else try {
super.withFixture(test)
}
catch {
case ex: TestPendingException =>
throw ex
case NonFatal(t: Throwable) =>
anyFailure = true
throw t
}
}
}
I don't know about a ready made solution. But you can fairly easily write your own Fixtures.
See "Composing stackable fixture traits" in the javadoc of the Suite trait
Such a fixture could for example replace all test executions after the first one with calls to pending
You can use trait org.scalatest.CancelAfterFailure to cancel remaining tests after first failure:
import org.scalatest._
class MySpec extends FunSuite with CancelAfterFailure {
test("successfull test") {
succeed
}
test("failed test") {
assert(1 == 0)
}
test("this test and all others will be cancelled") {
// ...
}
}
Related
There is a main class named 'MainProcess.scala' that I'm running some test cases for it. I wanno write an end to end test for this class to validate it's functionality.
Th problem here is the end to end test requires some criteria has to be established to be able to test whole functionality. For instance:
class MainProcess() {
def foo(someparams):Future[Boolean] = {
if criteria true else false
}
def bee(some params):Future[WSResponse] = {
// call a micro service
}
}
My question is: Is it a good practice to mock 'foo' method such that it always return true or mock 'bee' method so that test can pass through these modules and continue till it reaches the point I intend to see it's result. As I am testing this class, I know that mocking the same class results into error or malfunctionality of test case:
private def guiceApplicationBuilder(app: Application): Application = {
new GuiceApplicationBuilder()
.overrides(bind[MainProcess].toInstance(mainProcessMock))
.build()
}
If this is not a good practice, so how to do such stuff to mock specific modules of main class?
Thank you in advance.
In my Scalatra routes, I often use halt() to fail fast:
val user: User = userRepository.getUserById(params("userId"))
.getOrElse {
logger.warn(s"Unknown user: $userId")
halt(404, s"Unknown user: $userId")
}
As shown in the example, I also want to log a warning in those cases. But I'd like to avoid the code duplication between the halt() and the logger. It would be a lot cleaner to simply do:
val user: User = userRepository.getUserById(params("userId"))
.getOrElse(halt(404, s"Unknown user: $userId"))
What would be the best way of logging all "HaltExceptions" in a cross-cutting manner ?
I've considered:
1) Overriding the halt() method in my route:
override def halt[T](status: Integer, body: T, headers: Map[String, String])(implicit evidence$1: Manifest[T]): Nothing = {
logger.warn(s"Halting with status $status and message: $body")
super.halt(status, body, headers)
}
Aside from the weird method signature, I don't really like this approach, because I could be calling the real halt() by mistake instead of the overridden method, for example if I'm halting outside the route. In this case, no warning would be logged.
2) Use trap() to log all error responses:
trap(400 to 600) {
logger.warn(s"Error returned with status $status and body ${extractBodyInSomeWay()}")
}
But I'm not sure it's the best approach, especially since it adds 201 routes to the _statusRoutes Map (one mapping for each integer in the range...). I also don't know how to extract the body here ?
3) Enable some kind of response logging in Jetty for specific status codes ?
What would be the best approach to do this? Am I even approaching this correctly?
The easiest solution is doing it in a servlet filter like below:
package org.scalatra.example
import javax.servlet._
import javax.servlet.http.HttpServletResponse
class LoggingFilter extends Filter {
override def init(filterConfig: FilterConfig): Unit = ()
override def destroy(): Unit = ()
override def doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain): Unit = {
chain.doFilter(request, response)
val status = response.asInstanceOf[HttpServletResponse].getStatus
if (status >= 400 && status <= 600) {
// Do logging here!
}
}
}
Register this filter in your Bootstrap class (or it's possible even in web.xml):
package org.scalatra.example
import org.scalatra._
import javax.servlet.ServletContext
class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext): Unit = {
context.addFilter("loggingFilter", new LoggingFilter())
context.getFilterRegistration("loggingFilter")
.addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/*")
// mount your servlets or filters
...
}
}
In my opinion, Scalatra should provide a way to trap halting easier essentially. In fact, there is a method named renderHaltException in ScalatraBase, it looks to be possible to add logging by overriding this method at a glance:
https://github.com/scalatra/scalatra/blob/cec3f75e3484f2233274b1af900f078eb15c35b1/core/src/main/scala/org/scalatra/ScalatraBase.scala#L512
However we can't do it actually because HaltException is package private and it can be accessed inside of org.scalatra package only. I wonder HaltException should be public.
I'm trying to make scalaTestPlus work in my play application (I'm using Play! 2.2). It works well until I need a function coming from my application. For instance, if I run this very simple test (by launching in a sbt console "test-only TestName"):
import org.scalatestplus.play._
import org.scalatest._
import Matchers._
class Test extends PlaySpec {
"This test" must {
"run this very simple test without problem" in {
1 mustEqual 1
}
}
}
There is no problem, but as soon as I call a function from my app, like in this code:
class Test extends PlaySpec {
"This test" must {
"run this very simple test without problem" in {
models.Genre.genresStringToGenresSet(Option("test")) //Here is the problem
1 mustEqual 1
}
}
}
I get an error: java.lang.ExceptionInInitializerError: at... Cause: java.lang.RuntimeException: There is no started application (even if my application is running).
I'm probably missing something simple since I'm brand new to ScalaTest, so any help abut what I'm doing wrong would be apreciated ;)
You may need an application in scope when using PlaySpec, as some operations assume that there is a Play application available via Play.current:
class Test extends PlaySpec {
implicit override lazy val app: FakeApplication = FakeApplication(...)
"This test" must {
"run this very simple test without problem" in {
models.Genre.genresStringToGenresSet(Option("test")) //Here is the problem
1 mustEqual 1
}
}
}
Check the functional testing documentation for more details on FakeApplication.
However, I don't think you need should need this for model testing. In the normal ScalaTest docs for play, it seems to just mix inMockitoSugar. But your method call chain may invoke some global state of Play that does require an Application, in which case the FakeApplication is the way to go
As asked by #akauppi, here is a method that works perfectly for me:
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
class A extends PlaySpec with OneAppPerSuite {
"a" must {
"return true" in {
//thanks to with OneAppPerSuite, it now works
models.Genre.genresStringToGenresSet(Option("test"))
1 mustBe 1
}
"return false" in {
1 mustBe 2
}
}
}
And I simply launch the test with sbt ~testOnly a
I have a Specs2RouteTest
"test a route with some modified dependencies" in {
bindingModule.modifyBindings { implicit module =>
module.bind[AuthorizationService].toModuleSingle { createMockAuthService("1") }
val req = createMockRequest("1")
val testApi = module.inject [ApiEndpoints](None)
Post(s"/api/v1/service", JsonEntity(req.toJson)) ~> testApi.routes ~> check {
....
}
}
}
I confirm that the modified binding is set within the test. But once it gets into the route I am back to seeing the bindings as set up in the test module. Generally this modifyBindings{} technique seems to work to keep tests isolated and when I'm doing unit testing I can swap out dependencies no problem... but on these integration tests I can't seem to make the route under test pick up any binding modifications. Am I doing something obviously wrong?
I need to put one of my test cases into a "pending" state.
I would like to assing some sort of message to it that can be displayed on the output when running the test, something like JUnit with #Ignore("Pending: issue #1234 needs to be fixed").
Is there an equivalent for that with Specs2?
class MySpec extends mutable.Specification {
args(skipAll = true) // Can I include a message here in the output somehow?
"cool MyClass feature" should {
"which is broken unfortunately" in {
failure
}
}
}
Thanks in advance!
For an individual example, I believe you can use:
class MySpec extends mutable.Specification {
"cool MyClass feature" should {
"which is broken unfortunately" in {
failure
}.pendingUntilFixed("message about the issue")
}
}
I don't know if there's a way to extend this to mark all the examples in a spec as pending with the same message, as you seem to be hoping.