Difference between revisions of "Googletest"

From Apache OpenOffice Wiki
Jump to: navigation, search
(add some basic info for gtest)
Line 4: Line 4:
  
 
Unit tests will be enabled by default in the future but can be disabled during '''configure'''. Simply add the '''--disable-unit-tests''' to your configure command.
 
Unit tests will be enabled by default in the future but can be disabled during '''configure'''. Simply add the '''--disable-unit-tests''' to your configure command.
 
  
 
==How to use gtest==
 
==How to use gtest==
gtest is not yet fully integrated in gbuild but available in dmake makefiles. If we will continue with gbuild a better and direct integration have to be provided. But for now you can easy use with a dmake file. Up to 9 run unit test targets are supported and you can easy enable or disable the execution of one test.
+
gtest is not yet fully integrated in gbuild but available in dmake makefiles. If we will continue with gbuild a better and direct integration have to be provided. But for now you can easy use it with a dmake makefile. Up to 9 run unit test targets are supported and you can easy enable or disable the execution of one 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 38: Line 37:
 
</source>
 
</source>
  
The whole test (directory) gets build 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'''
+
The whole test (directory) gets build 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'''.
 +
 
 +
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>

Revision as of 11:31, 30 May 2014

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 will be enabled by default in the future but can be disabled during configure. Simply add the --disable-unit-tests to your configure command.

How to use gtest

gtest is not yet fully integrated in gbuild but available in dmake makefiles. If we will continue with gbuild a better and direct integration have to be provided. But for now you can easy use it with a dmake makefile. Up to 9 run unit test targets are supported and you can easy enable or disable the execution of one 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

The whole test (directory) gets build 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.

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();
}
Personal tools