1
0
mirror of https://github.com/actix/actix-website synced 2024-12-18 09:53:12 +01:00
actix-website/docs/testing.md

45 lines
2.5 KiB
Markdown
Raw Permalink Normal View History

2018-05-22 23:15:08 +02:00
---
title: Testing
---
import CodeBlock from "@site/src/components/code_block";
2018-05-22 23:15:08 +02:00
# Testing
Every application should be well tested. Actix Web provides tools to perform integration tests against your applications and unit test tools for custom extractors and middleware.
2018-05-22 23:15:08 +02:00
Actix Web provides a request builder type. [_TestRequest_][testrequest] implements a builder-like pattern. You can generate a `HttpRequest` instance with `to_http_request()` and call your handlers or extractors with it. Also see
2018-05-22 23:15:08 +02:00
## Integration Testing For Applications
2018-05-22 23:15:08 +02:00
2022-04-07 16:44:10 +02:00
There are a few methods for testing your application. Actix Web can be used to run the application with specific handlers in a real HTTP server.
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
`TestRequest::get()`, `TestRequest::post()` and other methods can be used to send requests to the test server.
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
To create a `Service` for testing, use the `test::init_service` method which accepts a regular `App` builder.
2018-05-22 23:15:08 +02:00
2020-09-12 17:21:54 +02:00
> Check the [API documentation][actixdocs] for more information.
2018-05-22 23:15:08 +02:00
<CodeBlock example="testing" file="integration_one.rs" section="integration-one" />
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01: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
<CodeBlock example="testing" file="integration_two.rs" section="integration-two" />
2018-05-22 23:15:08 +02:00
## Stream Response Testing
2018-05-23 22:35:42 +02:00
2023-02-03 20:11:14 +01:00
If you need to test stream generation, it would be enough to call [`into_parts()`][resintoparts] and convert the resulting body into a future and execute it, for example when testing [_Server Sent Events_][serversentevents].
2018-05-23 22:35:42 +02:00
<CodeBlock example="testing" file="stream_response.rs" section="stream-response" />
## Unit Testing Extractors
Unit testing has pretty limited value for applications, but can be useful when developing extractors, middleware, and responders. Given that, calling directly into handler functions **which are defined stand-alone, without using routing macros** (like `#[get("/")]`) is possible if you want to make assertions on custom `Responder`s.
<CodeBlock example="testing" file="main.rs" section="unit-tests" />
[serversentevents]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
2023-02-03 20:11:14 +01:00
[resintoparts]: https://docs.rs/actix-web/4/actix_web/struct.HttpResponse.html#method.into_parts
2022-03-06 00:55:35 +01:00
[actixdocs]: https://docs.rs/actix-web/4/actix_web/test/index.html
[testrequest]: https://docs.rs/actix-web/4/actix_web/test/struct.TestRequest.html