You are here: Foswiki>FESA Web>Fesa3version1>FESA3GoogleTesting100 (28 Nov 2024, DominicDay)Edit Attach

Testing FESA3 equipment software with googletest

This page summarizes how to create and run tests with FESA and googletest

Googletest

For an overview of googletest, see the overview at https://github.com/google/googletest

And the documentation at https://github.com/google/googletest/blob/master/googletest/docs/primer.md

Unit Testing

Unit tests are for testing the internal logic of your class. A gtest binary is built and linked with the Class only, not the Deploy unit. All objects being tested must be instantiated or mocked in the setup phase. No FESA background processes will be running, so it is not possible to test e.g. field synchronization or notifications.

Integration Testing

Integration tests exercise the external interface. A complete Deploy Unit and a gtest binary are built. The gtest binary tests the RDA interface of the Deploy Unit to verify correct operation. If the hardware interface is mocked and simulated timing is used, integration tests can run on a development machine / VM / build server. Otherwise a FEC is needed.

Googletest instalation on asl cluster

  • gtest is installed in /common/usr/fesa/tools/google/gtest
  • gmock is installed in /common/usr/fesa/tools/google/gmock
  • 1.6.0 and 1.10.0 available
Googletest and FESA makefiles

Common build makefiles >= 2.13.2 ( >= FESA3 7.0.0) support building gtest unit tests (see Make.generic)

Test source files must be in directory "test"

The environment variables GTEST and GMOCK must be set to "true", otherwise the common build makefile will not offer the test targets

Build tests with make test

Run tests with make run-test

Gtest and FESA Unit Test "Hello World"

In your FESA project's "src" directory, create a subdirectory "test".

Create the file src/test/My_unittest.cpp:
#include <gtest/gtest.h>
TEST(MyTest, testCase1)
{
  ASSERT_EQ(1,1);
}

In the project directory, enter the command:
GTEST=true make test

(The make targets test and run-test are inactive unless GTEST=true)

To use an alternate googletest version

GTEST=true GTEST_VERSION=1.10.0

Alternatively, in Eclipse create a new build target with the same command.

If successful, the build will produce a binary such as test/bin/x86_64/gtest_fesa-class-MyClass

To run the test, in the project directory, enter the command:
GTEST=true make run-test

This should produce output similar to:
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN      ] MyTest.testCase1
[       OK ] MyTest.testCase1 (0 ms)
[----------] 1 test from MyTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.


Starting the gtest binary directly allows for additional options such as repeated tests:

test/bin/x86_64/gtest_fesa-class-MyClass --gtest_repeat=10

Testing a c++ class with setup / teardown fixture

Most classes will require initialization before testing. This can be performed in each test, or in a test fixture that is repeated for all tests.

Given a c++ class defined in src/Common/TestMe.cpp / .h that you would like to test:

class TestMe {
public:
 TestMe(int);
 int get() const;
 void add(int);
...

Create the file src/test/TestMe_unittest.cpp:

#include <gtest/gtest.h>
#include <MyClass/Common/TestMe.h>
class TestMeTest : public ::testing::Test
{
public:
TestMe *testme;

void SetUp()
{
 testme = new TestMe(1);
}
void TearDown()
{
 delete testme;
}
TEST_F(TestMeTest, testAddMethod)
{
 ASSERT_EQ(1,testme->get());
 testme->add(1);
 ASSERT_EQ(2,testme->get());
}
TEST_F(TestMeTest, testAddMethod2)
{
 ASSERT_EQ(1,testme->get());
 testme->add(2);
 ASSERT_EQ(3,testme->get());
}
};

build and run with the command / build targets
GTEST=true make test && GTEST=true make run-test

The output should be similar to:
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from TestMeTest
[ RUN      ] TestMeTest.testAddMethod
[       OK ] TestMe.testAddMethod (0 ms)
[ RUN      ] TestMe.testAddMethod2
[       OK ] TestMe.testAddMethod2 (0 ms)
[----------] 2 tests from TestMeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[  PASSED  ] 2 tests.

Testing Exceptions
Topic revision: r8 - 28 Nov 2024, DominicDay
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback