org.scalatest.AsyncSuite.scala Maven / Gradle / Ivy
/*
* Copyright 2001-2014 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
import scala.concurrent.Future
import org.scalatest.exceptions.StackDepthExceptionHelper.getStackDepthFun
import scala.concurrent.ExecutionContext
import scala.language.implicitConversions // To convert Assertion to Future[Assertion]
trait AsyncSuite extends Suite { thisAsyncSuite =>
/**
* An implicit execution context used by async styles to transform Future[Assertion]
values
* returned by tests into Future[Outcome]
values, and can be used within the async tests themselves,
* for example, when mapping assertions onto futures.
*/
implicit def executionContext: ExecutionContext
/**
* Implicitly converts an Assertion
to a Future[Assertion]
.
*
*
* This implicit conversion is used to allow synchronous tests to be included along with
* asynchronous tests in an AsyncSuite
. It will be
*
*
* @param assertion the Assertion
to convert
* @return a Future[Assertion]
that has already completed successfully
* (containing the Succeeded
singleton).
*/
implicit def convertAssertionToFutureAssertion(assertion: Assertion): Future[Assertion] = Future.successful(assertion)
protected[scalatest] def parallelAsyncTestExecution: Boolean = thisAsyncSuite.isInstanceOf[org.scalatest.ParallelTestExecution] ||
thisAsyncSuite.isInstanceOf[org.scalatest.RandomTestOrder]
/**
* Throws NotAllowedException
, because its signature assumes synchronous testing. Use withAsyncFixture
instead.
*/
final override def withFixture(test: NoArgTest): Outcome = {
throw new exceptions.NotAllowedException(FailureMessages.withFixtureNotAllowedInAsyncFixtures, getStackDepthFun("AsyncSuite.scala", "withFixture"))
}
/**
* A test function taking no arguments and returning a Future[Outcome]
.
*
*
* For more detail and examples, see the relevant section in the
* documentation for trait AsyncFlatSpec
.
*
*/
trait NoArgAsyncTest extends (() => Future[Outcome]) with TestData {
/**
* Runs the body of the test, returning a Future[Outcome]
.
*/
def apply(): Future[Outcome]
}
/**
* Run the passed test function in the context of a fixture established by this method.
*
*
* This method should set up the fixture needed by the tests of the
* current suite, invoke the test function, and if needed, register a callback
* on the resulting Future[Outcome]
to perform any clean
* up needed after the test completes. Because the NoArgTest
function
* passed to this method takes no parameters, preparing the fixture will require
* side effects, such as reassigning instance var
s in this Suite
or initializing
* a globally accessible external database. If you want to avoid reassigning instance var
s
* you can use fixture.AsyncSuite.
*
*
*
* This trait's implementation of runTest
invokes this method for each test, passing
* in a NoArgAsyncTest
whose apply
method will execute the code of the test
* and returns its result.
*
*
*
* This trait's implementation of this method simply invokes the passed NoArgAsyncTest
function.
*
*
* @param test the no-arg async test function to run with a fixture
*/
def withAsyncFixture(test: NoArgAsyncTest): Future[Outcome] = {
test()
}
}