hurl-test
Bibliothèque Rust qui exécute des scénarios Hurl sur une API depuis cargo test.
RôleAuteur
Type de projetBibliothèque Rust / tests HTTP
ÉquipeOpen source
Tests HTTP avec Hurl
hurl-test est une bibliothèque Rust qui exécute des scénarios Hurl depuis cargo test. Les tests envoient des requêtes HTTP à l’API et font échouer la suite si une assertion ne passe pas.
POST {{base_url}}/notesContent-Type: application/json{ "title": " Buy milk "}HTTP 201# Reuse the server-assigned ID in the following requests.[Captures]note_id: jsonpath "$.id"[Asserts]jsonpath "$.id" > 0jsonpath "$.title" == "Buy milk"GET {{base_url}}/notes/{{note_id}}HTTP 200[Asserts]jsonpath "$.id" == {{note_id}}jsonpath "$.title" == "Buy milk"GET {{base_url}}/notesHTTP 200[Asserts]jsonpath "$" count >= 1jsonpath "$[?(@.id == {{note_id}})].title" == "Buy milk"Lancer un serveur dans le test
L’exemple démarre le serveur sur un port disponible avant d’exécuter les scénarios. Le même test peut ainsi tourner localement ou dans un runner CI sans préparation manuelle du serveur.
use axum::serve;use hurl_test::TestCases;use miette::{IntoDiagnostic, Result};use notes_api::app;use tokio::{net::TcpListener, spawn};// Hurl blocks this test's thread; another worker keeps the server responsive.#[tokio::test(flavor = "multi_thread", worker_threads = 2)]async fn ui() -> Result<()> { let listener = TcpListener::bind("127.0.0.1:0").await.into_diagnostic()?; let address = listener.local_addr().into_diagnostic()?; // Dropping the test runtime stops the server, including after a test failure. spawn(async move { serve(listener, app()).await }); TestCases::new() .variable("base_url", format!("http://{address}")) .ignore("tests/hurl/templates/**")? .include("tests/hurl/**/*.hurl")? .run()?; Ok(())}