visual unitarias unit test studio pruebas las desventajas unit-testing googletest parameterized-unit-test

unit testing - unitarias - Prueba de Google: ¿Existe una manera de combinar una prueba que sea tanto de tipo parametrizado como de valor parametrizado?



unit test c# (1)

Sé cómo desarrollar una prueba parametrizada de tipo y una prueba parametrizada de valor por separado. Lo que estoy tratando de averiguar es si es posible combinar ambos. En otras palabras, cree una prueba genérica que tome cualquier tipo y rango de valores para ese tipo.


No hay ninguna combinación lista para usar de pruebas de tipo parametrizado y pruebas de valor parametrizado. A los desarrolladores más inteligentes se les ha preguntado la pregunta y dijeron que No.

Sin embargo, hay una forma simple y rutinaria (como lo sugiere Zhanyong Wan en la discusión vinculada) en la que puede crear su propio caso de prueba parametrizado por tipo que pruebe alguna condición para un rango específico de valores del tipo de parámetro. Aquí hay un ejemplo elemental donde la condición es solo mayor que 0 :

#include <vector> #include "gtest/gtest.h" template<class T> struct foo_test : public ::testing::Test { static std::vector<T> _range_; }; TYPED_TEST_CASE_P(foo_test); TYPED_TEST_P(foo_test, IsGreaterThanZero) { for (TypeParam value : foo_test<TypeParam>::_range_) { EXPECT_GT(value,0); } } REGISTER_TYPED_TEST_CASE_P(foo_test,IsGreaterThanZero); typedef ::testing::Types<char, int, float> MyTypes; INSTANTIATE_TYPED_TEST_CASE_P(My, foo_test, MyTypes); template<> std::vector<char> foo_test<char>::_range_{''1'',''2'',''3''}; template<> std::vector<int> foo_test<int>::_range_{1,2,3}; template<> std::vector<float> foo_test<float>::_range_{1.1,2.2,0.0}; int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }

Cuando se compila y ejecuta la salida de eso es:

[==========] Running 3 tests from 3 test cases. [----------] Global test environment set-up. [----------] 1 test from My/foo_test/0, where TypeParam = char [ RUN ] My/foo_test/0.IsGreaterThanZero [ OK ] My/foo_test/0.IsGreaterThanZero (0 ms) [----------] 1 test from My/foo_test/0 (0 ms total) [----------] 1 test from My/foo_test/1, where TypeParam = int [ RUN ] My/foo_test/1.IsGreaterThanZero [ OK ] My/foo_test/1.IsGreaterThanZero (0 ms) [----------] 1 test from My/foo_test/1 (0 ms total) [----------] 1 test from My/foo_test/2, where TypeParam = float [ RUN ] My/foo_test/2.IsGreaterThanZero /home/imk/develop/SO/gtest/main.cpp:14: Failure Expected: (value) > (0), actual: 0 vs 0 [ FAILED ] My/foo_test/2.IsGreaterThanZero, where TypeParam = float (0 ms) [----------] 1 test from My/foo_test/2 (1 ms total) [----------] Global test environment tear-down [==========] 3 tests from 3 test cases ran. (1 ms total) [ PASSED ] 2 tests. [ FAILED ] 1 test, listed below: [ FAILED ] My/foo_test/2.IsGreaterThanZero, where TypeParam = float 1 FAILED TEST

Los resultados tienen una granularidad más gruesa de lo que sería ideal: solo 3 pruebas en lugar de 9. Aún así, los valores fallidos se pueden informar, como se muestra, por lo que el grano grueso puede ser tolerable.