test google c++ unit-testing googletest

c++ - google - Cómo pasar parámetros al gtest



google test install (1)

Google Test solo reconoce sus propias opciones de línea de comandos. Cada vez que encuentra uno, lo elimina de argv y actualiza argc consecuencia, por lo que después de que InitGoogleTest regrese, todo lo que quede en argv estará disponible para que usted pueda procesarse. Use su técnica de análisis de línea de comandos favorita, almacene los resultados en alguna variable global y consúltela durante sus pruebas.

Si las opciones de una línea de comandos se parecen a una opción de prueba de Google pero realmente no lo son, el programa imprimirá su mensaje de ayuda y se cerrará sin ejecutar ninguna prueba. Las opciones de prueba de Google comienzan con gtest_ .

¿Cómo puedo pasar el parámetro a mis suites de prueba?

gtest --number-of-input=5

Tengo el siguiente código principal de gtest. Y --number-of-input=5 debe pasarse a InitGoogleTest ().

#include <iostream> #include <gtest/gtest.h> int main(int argc, char **argv) { std::cout << "Running main() from gtest_main.cc/n"; ::testing::GTEST_FLAG(output) = "xml:hello.xml"; testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }

¿No sé cómo pasar mi parámetro a las suites / casos de prueba de la siguiente manera?

class TestTwo : public QuickTest { protected: virtual void SetUp() { QuickTest::SetUp(); square = new Square(10); circle = new Circle(10); } virtual void TearDown() { delete square; delete circle; QuickTest::TearDown(); } Square* square; Circle* circle; }; // Now, let''s write tests using the QueueTest fixture. // Tests the default constructor. TEST_F(TestOne, DefaultConstructor) { EXPECT_EQ(100.0, square->area()); } TEST_F(TestOne, DefaultDestructor) { EXPECT_EQ(1,1); } TEST_F(TestOne, VHDL_EMIT_Passthrough) { EXPECT_EQ(1,1); } TEST_F(TestOne, VHDL_BUILD_Passthrough) { EXPECT_EQ(1,1); }

Adicional

InitGoogleTest() el método principal para mostrar el argv [i] después de InitGoogleTest() .

int main(int argc, char **argv) { std::cout << "Running main() from gtest_main.cc/n"; ::testing::GTEST_FLAG(output) = "xml:hello.xml"; testing::InitGoogleTest(&argc, argv); for (int i = 0; i < argc; i++) { cout << i << ":" << argv[i] << endl; }

Estos son los argumentos dados al gtest: ./s --number-of-input=5 --gtest_filter=Test_Cases1* .

Este es el resultado:

Running main() from gtest_main.cc 0:./s 1:--number-of-input=5 Note: Google Test filter = Test_Cases1* [==========] Running 0 tests from 0 test cases. [==========] 0 tests from 0 test cases ran. (0 ms total) [ PASSED ] 0 tests.

gtest filtra las pruebas que no tienen el nombre de Test_Cases1 , y también muestra los argumentos correctos además de aquellos que comienzan con gtest .

Referencia - Cómo ejecutar casos de prueba específicos en GoogleTest