You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/advanced/testing.md
+35-23Lines changed: 35 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,35 +56,26 @@ To ensure your tests run in a serialized manner (e.g., when testing with a datab
56
56
57
57
### Testable Application
58
58
59
-
Define a private method function `withApp` to streamline and standardize the setup and teardown for our tests. This method encapsulates the lifecycle management of the `Application` instance, ensuring that the application is properly initialized, configured, and shut down for each test.
59
+
To provide a streamlined and standardized setup and teardown of tests, `VaporTesting` offers the `withApp` helper function. This method encapsulates the lifecycle management of the `Application` instance, ensuring that the application is properly initialized, configured, and shut down for each test.
60
60
61
-
In particular it is important to release the threads the application requests at startup. If you do not call `asyncShutdown()`on the app after each unit test, you may find your test suite crash with a precondition failure when allocating threads for a new instance of `Application`.
61
+
Pass your application's `configure(_:)`method to the `withApp` helper function to make sure all your routes get correctly registered:
Pass the `Application` to your package's `configure(_:)` method to apply your configuration. Then you test the application calling the `test()` method. Any test-only configurations can also be applied.
79
-
80
71
#### Send Request
81
72
82
73
To send a test request to your application, use the `withApp` private method and inside use the `app.testing().test()` method:
83
74
84
75
```swift
85
76
@Test("Test Hello World Route")
86
77
funchelloWorld() asyncthrows {
87
-
tryawaitwithApp { app in
78
+
tryawaitwithApp(configure: configure) { app in
88
79
tryawait app.testing().test(.GET, "hello") { res async in
Configure the database specifically for testing to ensure that your live database is never used during tests.
125
+
Configure the database specifically for testing to ensure that your live database is never used during tests. For example, when you are using SQLite, you could configure your database in the `configure(_:)` function as follows:
By combining these methods, you can ensure that each test starts with a fresh and consistent database state, making your tests more reliable and reducing the likelihood of false positives or negatives caused by lingering data.
139
+
!!! warning
140
+
Make sure you run your tests against the correct database, so you prevent accidentally overwriting data you do not want to lose.
143
141
144
-
Here's how the `withApp`function looks with the updated configuration:
142
+
Then you can enhance your tests by using `autoMigrate()` and `autoRevert()` to manage the database schema and data lifecycle during testing. To do so, you should create your own helper function `withAppIncludingDB` that includes the database schema and data lifecycles:
tryawait app.testing().test(.GET, "hello") { res async in
167
+
#expect(res.status== .ok)
168
+
#expect(res.body.string=="Hello, world!")
169
+
}
170
+
}
171
+
}
172
+
```
173
+
174
+
By combining these methods, you can ensure that each test starts with a fresh and consistent database state, making your tests more reliable and reducing the likelihood of false positives or negatives caused by lingering data.
175
+
176
+
165
177
## XCTVapor
166
178
167
179
Vapor includes a module named `XCTVapor` that provides test helpers built on `XCTest`. These testing helpers allow you to send test requests to your Vapor application programmatically or running over an HTTP server.
0 commit comments