2018-05-22 23:15:08 +02:00
|
|
|
---
|
|
|
|
title: Testing
|
|
|
|
menu: docs_advanced
|
|
|
|
weight: 210
|
|
|
|
---
|
|
|
|
|
|
|
|
# Testing
|
|
|
|
|
2019-06-24 23:43:31 +02:00
|
|
|
Every application should be well tested. Actix-web provides tools to perform unit and
|
2018-05-22 23:15:08 +02:00
|
|
|
integration tests.
|
|
|
|
|
|
|
|
# Unit Tests
|
|
|
|
|
2019-06-24 23:43:31 +02:00
|
|
|
For unit testing, actix-web provides a request builder type and a simple handler runner.
|
2019-06-25 05:36:32 +02:00
|
|
|
[*TestRequest*][testrequest] implements a builder-like pattern. You can generate a
|
|
|
|
`HttpRequest` instance with `to_http_request()`, or you can
|
2019-06-17 21:39:58 +02:00
|
|
|
run your handler with `block_on()`.
|
|
|
|
|
|
|
|
{{< include-example example="testing" file="main.rs" section="unit-tests" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
# Integration tests
|
|
|
|
|
2019-06-24 23:43:31 +02:00
|
|
|
There a few methods for testing your application. Actix-web can be used
|
2018-05-22 23:15:08 +02:00
|
|
|
to run the application with specific handlers in a real http server.
|
|
|
|
|
2019-06-24 23:43:31 +02:00
|
|
|
`TestRequest::get()`, `TestRequest::post()`, and `TestRequest::client()`
|
2018-05-22 23:15:08 +02:00
|
|
|
methods can be used to send requests to the test server.
|
|
|
|
|
2019-10-07 16:47:46 +02:00
|
|
|
To create a `Service` for testing, use the `test::init_service` method which accepts a
|
2019-06-24 23:43:31 +02:00
|
|
|
regular `App` builder.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-25 05:36:32 +02:00
|
|
|
> Check the [api documentation][actixdocs] for more information.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-17 21:39:58 +02:00
|
|
|
{{< include-example example="testing" file="integration_one.rs" section="integration-one" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-10-14 01:58:48 +02:00
|
|
|
Note: If you get the error message "no Task is currently running" when running your
|
|
|
|
test, you may need to wrap your service call in
|
|
|
|
[test::run_on](https://docs.rs/actix-web/1/actix_web/test/fn.run_on.html):
|
|
|
|
|
|
|
|
```rust
|
|
|
|
let future = test::run_on(|| app.call(req));
|
|
|
|
let resp = test::block_on(future).unwrap();
|
|
|
|
```
|
|
|
|
|
2019-06-24 23:43:31 +02:00
|
|
|
If you need more complex application configuration testing should be very similar to creating
|
|
|
|
the normal application. For example, you may need to initialize application state. Create an
|
|
|
|
`App` with a `data` method and attach state just like you would from a normal application.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-17 21:39:58 +02:00
|
|
|
{{< include-example example="testing" file="integration_two.rs" section="integration-two" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2018-05-23 22:35:42 +02:00
|
|
|
# Stream response tests
|
|
|
|
|
2019-06-25 05:36:32 +02:00
|
|
|
If you need to test stream it would be enough to convert a [*ClientResponse*][clientresponse]
|
|
|
|
to future and execute it.
|
|
|
|
For example of testing [*Server Sent Events*][serversentevents].
|
2018-05-23 22:35:42 +02:00
|
|
|
|
2019-06-17 21:39:58 +02:00
|
|
|
{{< include-example example="testing" file="stream_response.rs" section="stream-response" >}}
|
2019-06-25 05:36:32 +02:00
|
|
|
|
|
|
|
[serversentevents]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
|
2019-12-25 09:23:43 +01:00
|
|
|
[clientresponse]: https://docs.rs/actix-web/1/actix_web/client/struct.ClientResponse.html
|
2019-12-20 18:31:30 +01:00
|
|
|
[actixdocs]: https://docs.rs/actix-web/1/actix_web/test/index.html
|
2019-12-25 09:23:43 +01:00
|
|
|
[testrequest]: https://docs.rs/actix-web/1/actix_web/error/trait.ResponseError.html#foreign-impls
|