Rocket: Hello World with tests in separate file

Rocker web testing

Part of the series about the Rocket web framework.

In the previous article where we saw how to write Hello World in Web Rocket we also included some test. It was more straight forward to include the tests in the, but in reality I think it is better to have them in separate files.

So in this article we see and example how to do it.



name = "hello-world-external-test-file"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at

rocket = "0.5.0"

These are the same as in the previous version.

The tests

mod test {

The tests from inside the mod test were moved to a separate file called src/


use rocket::http::Status;
use rocket::local::blocking::Client;

fn hello_world() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(response.into_string(), Some("Hello, world!".into()));

The code

The curly braces were also removed so this is the code:


extern crate rocket;

fn index() -> &'static str {
    "Hello, world!"

fn rocket() -> _ {
    rocket::build().mount("/", routes![index])

mod tests;

Related Pages

Rocket - web development with Rust


Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo