Skip to content

Commit

Permalink
XCTest documentation from PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
alemohamad committed Jan 17, 2025
1 parent 2b27308 commit 942f287
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/advanced/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ let package = Package(
)
```

!!! warning
Be sure to use the corresponding testing module, as failing to do so can result in Vapor test failures not being properly reported.

Then, add `import VaporTesting` and `import Testing` at the top of your test files. Create structs with a `@Suite` name to write test cases.

```swift
Expand Down Expand Up @@ -157,3 +160,40 @@ private func withApp(_ test: (Application) async throws -> ()) async throws {
try await app.asyncShutdown()
}
```

## Using XCTest

If you prefer XCTest over the Swift Testing framework, or if a project requires compatibility with a more traditional approach, XCTest is fully supported. Here’s how you can get started:

First ensure that the `XCTVapor` module has been added to your package's test target.

```swift
let package = Package(
...
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0")
],
targets: [
...
.testTarget(name: "AppTests", dependencies: [
.target(name: "App"),
.product(name: "XCTVapor", package: "vapor"),
])
]
)
```

Then, add `import XCTVapor` at the top of your test files. Create classes extending `XCTestCase` to write tests cases.

```swift
import XCTVapor

final class MyTests: XCTestCase {
func testStub() throws {
// Test here.
}
}
```

!!! note
For newer projects or teams adopting Swift concurrency, `VaporTesting` is highly recommended due to its simplicity and integration with Vapor.

0 comments on commit 942f287

Please sign in to comment.