Section IV - integrated testing
We now have one last step, which is to add a function that will provide the service object. This is will not be covered by unit testing and is instead should be covered by integrated testing.
To create integrated tests, first create a new file named web-service-tests.rs
in a new directory named tests in the root path (same level as the src directory). Cargo will automatically parse the tests directory and run any tests that are located in any files located here.
In order to execute our service test, we will first need to include the actix-rt
library to our project. We do this by adding the line actix-rt = "1.1"
in the [dev-dependencies]
section of the Cargo.toml
file.
Once the library has been included in the Manifest, we define which libraries are required in the web-service-tests module by adding the following lines at the top of the web-service-tests.rs
file.
The extern
declarations specify the dependent crates (or libraries) that will be used in the web-service-tests module.
We then declare the bindings (or shortcuts) to a resources that will be using in the web-service-tests module. This is done by adding the following use
declarations below the extern
crate declarations.
Now we can add the code for our Hello World service test, which is added below the use
declarations.
At this point the web-service-tests.rs
file should look like this:
Try running your test with the cargo test
command. There should now be a line in the results referencing that the web_service_tests
has run.
Last updated
Was this helpful?