Function
withApp(environment:configuration:configReader:logger:services:configure:_:)
Perform a test while handling lifecycle of the application. Feel free to create a custom function like this, tailored to your project.
@discardableResult func withApp<T>(environment: Environment = .testing, configuration: ServerConfiguration = .init(), configReader: ConfigReader = ConfigReader(providers: [CommandLineArgumentsProvider(), EnvironmentVariablesProvider()]), logger: Logger = Logger.current, services: Application.ServiceConfiguration = .init(), configure: ((Application) async throws -> Void)? = nil, _ test: (Application) async throws -> T) async throws -> T
Parameters
configureA closure where you can register routes, databases, providers, and more.
testA closure which performs your actual test with the configured application.
Discussion
Usage:
@Test
func helloWorld() async throws {
try await withApp(configure: configure) { app in
try await app.testing { client in
let res = try await client.get("hello")
#expect(res.status == .ok)
try #expect(await res.body.requireString() == "Hello, world!")
})
}
}