Enredado con la prueba de Google

Tengo problemas para ponerme en funcionamiento con la prueba de Google. He leído los pasos sugeridos de Google, he mirado también un poste anterior, y leí algunos otros ejemplos , pero no despeja mucho cosas para arriba.

Para mantener las cosas simples, estoy intentando el ejemplo sugerido de la prueba de Google que está disponible desde el directorio en el Android ndk – sample1:

// main.cpp

#include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" #include "gtest/gtest.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); testing::InitGoogleTest(&argc, argv); QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/factorial/main.qml")); viewer.showExpanded(); return RUN_ALL_TESTS(); } 

// sample1_unittest.cpp

 #include <limits.h> #include "sample1.h" #include "gtest/gtest.h" // Tests factorial of 0. TEST(FactorialTest, Zero) { EXPECT_EQ(1, Factorial(0)); } 

Los archivos sample1.h, sample1.cpp también están en el proyecto, que contienen la función factorial. La prueba de Google fue informada igualmente al archivo del proyecto – factorial.pro:

 INCLUDEPATH += /opt/android-studio/ndk/sources/third_party/googletest/googletest/include 

Cuando presiono [Build> Build Project "factorial"] obtiene el siguiente error:

 main.cpp:8: error: undefined reference to 'testing::InitGoogleTest(int*, char**)' sample1_unittest.cpp:17: error: undefined reference to 'testing::Test::Test()' 

Estoy trabajando con Ubuntu, QtCreator, Android y C ++. De hecho he pasado ya 3 días burlándose alrededor, pero conseguir no mucho en cualquier lugar hasta ahora. Por lo tanto, estoy publicando aquí en la esperanza de algún gurú puede dar alguna pista sobre esto. Cualquier ayuda será apreciada sobre todo.

Parece que no has construido Google Test a partir de lo que describes. Necesita compilar el proyecto en una biblioteca y luego vincularlo. Si tiene CMake instalado, tiene dos opciones:

  • Utilice la GUI de CMake (es bastante intuitivo) para generar los archivos del sistema de compilación y, a continuación, utilizarlos como de costumbre (por ejemplo, si genera una solución de Visual Studio, abre el archivo .sln y crea el proyecto).
  • Utilice la línea de comandos para hacer lo mismo; Esencialmente, acaba de crear un nuevo directorio y hacer cmake <path-to-google-test> dentro de él. El resto es el mismo.

También puede construir la biblioteca por sí mismo. La distribución contiene una carpeta denominada fused-src que debe contener al menos dos archivos: gtest_main.cpp y gtest-all.cpp . Compile esos archivos y ya está. Necesita generar dos bibliotecas aquí: gtest de gtest-all.cpp y gtest_main fuera de gtest_main.cpp .

Otra alternativa sería conseguir bibliotecas ya construidas. Nunca los he buscado, pero podrían estar ahí afuera.

Intente algo como esto:

 $ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cpp 

Para más detalles:

  • Cómo configurar googleTest como una biblioteca compartida en Linux

Si todavía no funciona, puede resultar interesante considerar utilizar Makefile:

 # A sample Makefile for building Google Test and using it in user # tests. Please tweak it to suit your environment and project. You # may want to move it to your project's root directory. # # SYNOPSIS: # # make [all] - makes everything. # make TARGET - makes the given target. # make clean - removes all files generated by make. # Please tweak the following variable definitions as needed by your # project, except GTEST_HEADERS, which you can use in your own targets # but shouldn't modify. # Points to the root of Google Test, relative to where this file is. # Remember to tweak this if you move this file. GTEST_DIR = .. # Where to find user code. USER_DIR = ../samples # Flags passed to the preprocessor. # Set Google Test's header directory as a system directory, such that # the compiler doesn't generate warnings in Google Test headers. CPPFLAGS += -isystem $(GTEST_DIR)/include # Flags passed to the C++ compiler. CXXFLAGS += -g -Wall -Wextra -pthread # All tests produced by this Makefile. Remember to add new tests you # created to the list. TESTS = sample1_unittest # All Google Test headers. Usually you shouldn't change this # definition. GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \ $(GTEST_DIR)/include/gtest/internal/*.h # House-keeping build targets. all : $(TESTS) clean : rm -f $(TESTS) gtest.a gtest_main.a *.o # Builds gtest.a and gtest_main.a. # Usually you shouldn't tweak such internal variables, indicated by a # trailing _. GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS) # For simplicity and to avoid depending on Google Test's # implementation details, the dependencies specified below are # conservative and not optimized. This is fine as Google Test # compiles fast and for ordinary users its source rarely changes. gtest-all.o : $(GTEST_SRCS_) $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ $(GTEST_DIR)/src/gtest-all.cc gtest_main.o : $(GTEST_SRCS_) $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ $(GTEST_DIR)/src/gtest_main.cc gtest.a : gtest-all.o $(AR) $(ARFLAGS) $@ $^ gtest_main.a : gtest-all.o gtest_main.o $(AR) $(ARFLAGS) $@ $^ # Builds a sample test. A test should link with either gtest.a or # gtest_main.a, depending on whether it defines its own main() # function. sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS) $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \ $(USER_DIR)/sample1.h $(GTEST_HEADERS) $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc sample1_unittest : sample1.o sample1_unittest.o gtest_main.a $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@ 

Si tiene que usar Makefile para que gtest funcione, es probable que necesite ajustar la plantilla dada para su caso, ya que tiene la intención de crearla para usarla con Android.

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.