As part of my overall change over in Rookeries, from Python to Rust, I rewrote a suite of integration tests for the server API. To celebrate my successful transition, I released version 0.11.0 of Rookeries, whose tests use pure Rust now!
I found the rewrite not too cumbersome, thanks to the wonderful guide in the Rust book on integration tests. I did miss pytest’s fixture setup, which makes testing really easy. Especially when setting up fixtures that are run only once per test suite. That said, a single session setup makes it difficult to run tests in parallel. And while I ran into some inconsistent tests, I had the exact same kind of problems in Python. Basically my data model for Rookeries, doesn’t work well for singleton data like a single site. In the future, I plan on having a single Rookeries instance managing multiple different sites, so this point of not having a single test setup is ultimately moot.
Building a common setup module turned out to be more work than I imagined. I could not use macros because of the package setup. Also since tests are compiled individually, some methods in the common module, simply are not called, leading to warning of dead code.
What made the transition is easy was using the reqwest crate, and heavy use of serde_json’s json! macro. Comparing the Python and Rust version of the tests, makes them look very comparable.
Rust Version src:
#[test]
fn test_authenticated_user_cannot_modify_site_using_bad_request() {
test_site();
let api_base_uri = common::api_base_url();
let client = Client::new();
let auth_token = auth_token_headers();
let response = client
.put(api_base_uri.join("/api/site").unwrap())
.body("")
.header(Authorization(auth_token))
.send()
.unwrap();
assert_bad_request_response(response);
}
fn assert_bad_request_response(mut response: Response) {
let expected_response_json = json!({
"error": {
"status_code": 400,
"message": "Bad request",
}
});
assert_eq!(response.status(), StatusCode::BadRequest);
let actual_json_response = response.json::<Value>().unwrap();
assert_eq!(actual_json_response, expected_response_json);
}
Python Version src:
def test_authenticated_user_cannot_modify_site_using_bad_request(
auth_token_headers, api_base_uri, test_site):
response = requests.put(
url=f'{api_base_uri}/api/site',
data='',
headers=auth_token_headers,
)
assert_bad_request_response(response)
def assert_bad_request_response(response):
expected_response_json = {
'error': {
'status_code': http.HTTPStatus.BAD_REQUEST.value,
'message': mock.ANY,
}
}
assert response.json() == expected_response_json
assert response.status_code == http.HTTPStatus.BAD_REQUEST
I am pretty happy with how everything turned out overall. My next bit of Rookeries work will be migrating away from invoke and make to cargo make.