GoogleTest

O crate GoogleTest permite assertividade de testes flexĂ­vel usando matchers (correspondentes):

use googletest::prelude::*; #[googletest::test] fn test_elements_are() { let value = vec!["foo", "bar", "baz"]; expect_that!(value, elements_are!(eq("foo"), lt("xyz"), starts_with("b"))); }

Se mudarmos o Ășltimo elemento para "!", o teste falha com uma mensagem de erro estruturada apontando o erro:

---- test_elements_are stdout ---- Value of: value Expected: has elements: 0. is equal to "foo" 1. is less than "xyz" 2. starts with prefix "!" Actual: ["foo", "bar", "baz"], where element #2 is "baz", which does not start with "!" at src/testing/googletest.rs:6:5 Error: See failure output above

Speaker Notes

This slide should take about 5 minutes.
  • GoogleTest nĂŁo faz parte do playground do Rust, entĂŁo vocĂȘ precisa executar este exemplo em um ambiente local. Use cargo add googletest para adicionĂĄ-lo rapidamente a um projeto Cargo existente.

  • A linha use googletest::prelude::*; importa uma sĂ©rie de macros e tipos comumente usados.

  • Isso Ă© apenas a ponta do iceberg, existem muitos matchers embutidos. Considere passar pelo primeiro capĂ­tulo de “Testes avançados para aplicaçÔes Rust”, um curso de Rust autoguiado: ele fornece uma introdução guiada Ă  biblioteca, com exercĂ­cios para ajudĂĄ-lo a se sentir confortĂĄvel com os macros do googletest, seus matchers e sua filosofia geral.

  • Uma caracterĂ­stica particularmente interessante Ă© que as diferenças em strings de vĂĄrias linhas sĂŁo mostradas como uma diferença:

#[test] fn test_multiline_string_diff() { let haiku = "Memory safety found,\n\ Rust's strong typing guides the way,\n\ Secure code you'll write."; assert_that!( haiku, eq("Memory safety found,\n\ Rust's silly humor guides the way,\n\ Secure code you'll write.") ); }

mostra uma diferença usando cores (não mostradas aqui):

Value of: haiku Expected: is equal to "Memory safety found,\nRust's silly humor guides the way,\nSecure code you'll write." Actual: "Memory safety found,\nRust's strong typing guides the way,\nSecure code you'll write.", which isn't equal to "Memory safety found,\nRust's silly humor guides the way,\nSecure code you'll write." Difference(-actual / +expected): Memory safety found, -Rust's strong typing guides the way, +Rust's silly humor guides the way, Secure code you'll write. at src/testing/googletest.rs:17:5