org.scalatest.CancelAfterFailure.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.9.0 Show documentation
/*
* Copyright 2001-2011 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest
/**
* Trait that when mixed into a Suite
cancels any remaining tests in that
* Suite
instance after a test fails.
*
*
* The intended use case for this trait is if you have a suite of long-running tests that are
* related such that if one fails, you aren't interested in running the others, you can use this
* trait to simply cancel any remaining tests, so you need not wait long for them to complete.
*
*
*
* Note that this trait only cancels tests in the same Suite
instance, because
* it uses a private volatile instance variable as a flag to indicate whether or not a test has failed.
* If you are running each test in its own instance, therefore, it would not cancel the
* remaining tests, because they would not see the same flag. For this reason, this trait contains
* a final implementation of a method defined in OneInstancePerTest
,
* to prevent it from being mixed into any class that also mixes in OneInstancePerTest
,
* including by mixing in ParallelTestExecution
or a path traits.
*
*/
trait CancelAfterFailure extends SuiteMixin { this: Suite =>
@volatile private var cancelRemaining = false
/**
* Stackable implementation of withFixture
that cancels the current test if
* any previous test run in this Suite
instance has failed.
*/
abstract override def withFixture(test: NoArgTest) {
if (cancelRemaining) cancel("Canceled by CancelOnFailure because a test failed previously")
try super.withFixture(test)
catch {
case e: TestFailedException =>
cancelRemaining = true
throw e
}
}
/**
* Method defined to prevent this trait from being mixed into any class that also mixes in OneInstancePerTest
.
*/
final def newInstance: Suite with OneInstancePerTest = throw new UnsupportedOperationException
}