diff --git a/build.rs b/build.rs index 081d2b509..2346ab169 100644 --- a/build.rs +++ b/build.rs @@ -20,6 +20,7 @@ fn main() { "guide/src/qs_4_5.md", "guide/src/qs_5.md", "guide/src/qs_7.md", + "guide/src/qs_8.md", "guide/src/qs_9.md", "guide/src/qs_10.md", "guide/src/qs_12.md", diff --git a/guide/src/SUMMARY.md b/guide/src/SUMMARY.md index 275392118..8e78a0c3e 100644 --- a/guide/src/SUMMARY.md +++ b/guide/src/SUMMARY.md @@ -8,6 +8,7 @@ - [Errors](./qs_4_5.md) - [URL Dispatch](./qs_5.md) - [Request & Response](./qs_7.md) +- [Testing](./qs_8.md) - [WebSockets](./qs_9.md) - [Middlewares](./qs_10.md) - [Static file handling](./qs_12.md) diff --git a/guide/src/qs_8.md b/guide/src/qs_8.md new file mode 100644 index 000000000..be08e755e --- /dev/null +++ b/guide/src/qs_8.md @@ -0,0 +1,60 @@ +# Testing + +Every application should be well tested and. Actix provides the tools to perform unit and +integration tests. + +## Integration tests + +There are several methods how you can test your application. Actix provides +[*TestServer*](../actix_web/test/struct.TestServer.html) +server that could be used to run whole application of just specific handlers +in real http server. At the moment it is required to use third-party libraries +to make actual requests, libraries like [reqwest](https://crates.io/crates/reqwest). + +In simple form *TestServer* could be configured to use handler. *TestServer::new* method +accepts configuration function, only argument for this function is *test application* +instance. You can check [api documentation](../actix_web/test/struct.TestApp.html) +for more information. + +```rust +# extern crate actix_web; +extern crate reqwest; +use actix_web::*; +use actix_web::test::TestServer; + +fn index(req: HttpRequest) -> HttpResponse { + httpcodes::HTTPOk.response() +} + +fn main() { + let srv = TestServer::new(|app| app.handler(index)); // <- Start new test server + let url = srv.url("/"); // <- get handler url + assert!(reqwest::get(&url).unwrap().status().is_success()); // <- make request +} +``` + +Other option is to use application factory. In this case you need to pass factory function +same as you use for real http server configuration. + +```rust +# extern crate actix_web; +extern crate reqwest; +use actix_web::*; +use actix_web::test::TestServer; + +fn index(req: HttpRequest) -> HttpResponse { + httpcodes::HTTPOk.response() +} + +/// This function get called by http server. +fn create_app() -> Application { + Application::new() + .resource("/test", |r| r.h(index)) +} + +fn main() { + let srv = TestServer::with_factory(create_app); // <- Start new test server + let url = srv.url("/test"); // <- get handler url + assert!(reqwest::get(&url).unwrap().status().is_success()); // <- make request +} +```