Difference between revisions of "Googletest"
(Restructure page, separating the test example from the build system) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
What googletest (gtest) is and a general introduction how to use it can be found on the [https://code.google.com/p/googletest/ googletest] webpage. The [https://code.google.com/p/googletest/wiki/Documentation documentation] is very useful and provide some good examples, starting with a primer up to more comprehensive documentation for complex test. | What googletest (gtest) is and a general introduction how to use it can be found on the [https://code.google.com/p/googletest/ googletest] webpage. The [https://code.google.com/p/googletest/wiki/Documentation documentation] is very useful and provide some good examples, starting with a primer up to more comprehensive documentation for complex test. | ||
− | Unit tests | + | Unit tests are enabled by default but can be disabled during '''configure'''. Simply add the '''--disable-unit-tests''' to your configure command. |
==How to use gtest== | ==How to use gtest== | ||
− | + | ||
+ | The whole test (directory) gets built only if unit tests are not disabled globally during configure. If you have several test binaries in one directory you can easy disable the execution of one test by setting the variable '''APP1TEST=disable''' if dmake is used. | ||
+ | |||
+ | Details about writing tests can you find in the [https://code.google.com/p/googletest/wiki/Documentation documentation]. The only thing you have to do in your test source file is to include 'gtest/gtest.h' and insert a default main routine that triggers the tests you write before. | ||
+ | |||
+ | Example: | ||
+ | <source lang="cpp"> | ||
+ | #include "gtest/gtest.h" | ||
+ | |||
+ | TEST(TrueTest, PassingTest) { | ||
+ | EXPECT_EQ(1, 1); | ||
+ | EXPECT_NE(1, 2); | ||
+ | EXPECT_LT(1, 2); | ||
+ | EXPECT_LE(1, 2); | ||
+ | EXPECT_LE(1, 1); | ||
+ | EXPECT_GT(2, 1); | ||
+ | EXPECT_GE(2, 2); | ||
+ | } | ||
+ | |||
+ | TEST(FalseTest, FailureTest) { | ||
+ | EXPECT_EQ(1, 2); | ||
+ | } | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | ::testing::InitGoogleTest(&argc, argv); | ||
+ | return RUN_ALL_TESTS(); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ===dmake=== | ||
+ | |||
+ | gtest is easy to use with a dmake makefile. Up to 10 run unit test targets are supported (APP1TEST, APP2TEST, up to APP10TEST) and you can easy enable or disable the execution of each test. | ||
For example a simple makefile can look like this: | For example a simple makefile can look like this: | ||
<source lang="java"> | <source lang="java"> | ||
Line 37: | Line 69: | ||
</source> | </source> | ||
− | + | In the above example we build an '''salunittest''' and the test is executed during the build. By default the test is executed and a '''salunittest_result.xml''' is generated besides the test binary in the output directory. For now the these result xml files are not used, but maybe we will use it to collect all the test results and create some overview. | |
− | + | ===gbuild=== | |
− | + | In a gbuild module called '''sfx2''' in this example, first add this to '''Module_sfx2.mk''': | |
− | + | ||
− | + | ||
− | + | <source lang="java"> | |
− | + | ifeq ($(ENABLE_UNIT_TESTS),YES) | |
− | + | $(eval $(call gb_Module_add_check_targets,sfx2,\ | |
− | + | GoogleTest_sfx2_metadatable \ | |
− | + | )) | |
− | + | endif | |
− | + | </source> | |
− | + | ||
− | + | ||
− | + | Then in file '''GoogleTest_sfx2_metadatable.mk''' populate the name of the test, files to build, libraries to link to, etc.: | |
− | + | <source lang="java"> | |
− | + | $(eval $(call gb_GoogleTest_GoogleTest,sfx2_metadatable)) | |
− | + | $(eval $(call gb_GoogleTest_add_exception_objects,sfx2_metadatable, \ | |
− | + | sfx2/qa/gtest/test_metadatable \ | |
− | + | )) | |
− | + | ||
− | + | $(eval $(call gb_GoogleTest_add_linked_libs,sfx2_metadatable, \ | |
+ | sal \ | ||
+ | sfx \ | ||
+ | stl \ | ||
+ | $(gb_STDLIBS) \ | ||
+ | )) | ||
+ | |||
+ | $(eval $(call gb_GoogleTest_set_include,sfx2_metadatable,\ | ||
+ | $$(INCLUDE) \ | ||
+ | -I$(OUTDIR)/inc/offuh \ | ||
+ | -I$(OUTDIR)/inc \ | ||
+ | )) | ||
+ | |||
+ | $(eval $(call gb_GoogleTest_set_ldflags,sfx2_metadatable,\ | ||
+ | $$(LDFLAGS) \ | ||
+ | )) | ||
</source> | </source> | ||
− | |||
− | |||
[[Category:External]] | [[Category:External]] | ||
[[Category:Source directories]] | [[Category:Source directories]] | ||
[[Category:Test]] | [[Category:Test]] |
Latest revision as of 17:22, 22 October 2015
googletest is Google's framework for writing C++ tests (unit tests) on a variety of platforms. The intention is to use this test framework as replacement for cppunit that is license incompatible with Apache.
What googletest (gtest) is and a general introduction how to use it can be found on the googletest webpage. The documentation is very useful and provide some good examples, starting with a primer up to more comprehensive documentation for complex test.
Unit tests are enabled by default but can be disabled during configure. Simply add the --disable-unit-tests to your configure command.
How to use gtest
The whole test (directory) gets built only if unit tests are not disabled globally during configure. If you have several test binaries in one directory you can easy disable the execution of one test by setting the variable APP1TEST=disable if dmake is used.
Details about writing tests can you find in the documentation. The only thing you have to do in your test source file is to include 'gtest/gtest.h' and insert a default main routine that triggers the tests you write before.
Example:
#include "gtest/gtest.h" TEST(TrueTest, PassingTest) { EXPECT_EQ(1, 1); EXPECT_NE(1, 2); EXPECT_LT(1, 2); EXPECT_LE(1, 2); EXPECT_LE(1, 1); EXPECT_GT(2, 1); EXPECT_GE(2, 2); } TEST(FalseTest, FailureTest) { EXPECT_EQ(1, 2); } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
dmake
gtest is easy to use with a dmake makefile. Up to 10 run unit test targets are supported (APP1TEST, APP2TEST, up to APP10TEST) and you can easy enable or disable the execution of each test. For example a simple makefile can look like this:
PRJ = .. PRJNAME = sal TARGET = salunittest ENABLE_EXCEPTIONS = TRUE .INCLUDE: settings.mk .IF "$(ENABLE_UNIT_TESTS)" != "YES" all: @echo unit tests are disabled. Nothing to do. .ELSE xOBJFILES = $(APP1OBJS) APP1OBJS = $(OBJ)/unittest.obj APP1RPATH = NONE APP1STDLIBS = $(GTESTLIB) $(SALLIB) APP1TARGET = salunittest APP1TEST = enabled #APP1TEST = disabled .INCLUDE: target.mk .ENDIF
In the above example we build an salunittest and the test is executed during the build. By default the test is executed and a salunittest_result.xml is generated besides the test binary in the output directory. For now the these result xml files are not used, but maybe we will use it to collect all the test results and create some overview.
gbuild
In a gbuild module called sfx2 in this example, first add this to Module_sfx2.mk:
ifeq ($(ENABLE_UNIT_TESTS),YES) $(eval $(call gb_Module_add_check_targets,sfx2,\ GoogleTest_sfx2_metadatable \ )) endif
Then in file GoogleTest_sfx2_metadatable.mk populate the name of the test, files to build, libraries to link to, etc.:
$(eval $(call gb_GoogleTest_GoogleTest,sfx2_metadatable)) $(eval $(call gb_GoogleTest_add_exception_objects,sfx2_metadatable, \ sfx2/qa/gtest/test_metadatable \ )) $(eval $(call gb_GoogleTest_add_linked_libs,sfx2_metadatable, \ sal \ sfx \ stl \ $(gb_STDLIBS) \ )) $(eval $(call gb_GoogleTest_set_include,sfx2_metadatable,\ $$(INCLUDE) \ -I$(OUTDIR)/inc/offuh \ -I$(OUTDIR)/inc \ )) $(eval $(call gb_GoogleTest_set_ldflags,sfx2_metadatable,\ $$(LDFLAGS) \ ))