org.scalatest.CancelAfterFailure.scala Maven / Gradle / Ivy
/*
* Copyright 2001-2013 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 TestSuite
cancels any remaining tests in that
* TestSuite
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 TestSuite
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 trait.
*
*/
trait CancelAfterFailure extends TestSuiteMixin { this: TestSuite =>
@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): Outcome = {
if (cancelRemaining)
Canceled("Canceled by CancelOnFailure because a test failed previously")
else
super.withFixture(test) match {
case failed: Failed =>
cancelRemaining = true
failed
case outcome => outcome
}
}
/**
* 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
}