This repository is a simple illustration of gmock test using PID classes
and implementation of toy PID controller using the following dependecies:
- cmake
- gtest
- gmock
A virtual "generalPID" class is created to test the mocking framework interfaces in C++ using gmock. The virtual methods are inherited by the PID class where virtual computePID function is re-defined.
In the mockgeneralPID.h
file located in test folder all the methods used in gmocks are defined and are given below:
class mockgeneralPID : public generalPID {
public:
MOCK_METHOD2(computePID, float(float,float));
MOCK_METHOD1(setKP, bool(float kp));
MOCK_METHOD1(setKI, bool(float ki));
MOCK_METHOD1(setKD, bool(float kd));
};
In the testPID.cpp
file, all the tests are implemented with inline comments for reference.
To use gmock make the following changes
- Base cmake add the following line
add_subdirectory(vendor/googletest)
- test/cmake: Include the following lines at the end of the cmake
target_include_directories(cpp-test PUBLIC ../vendor/googletest/googletest/include PUBLIC ../vendor/googletest/googlemock/include ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(cpp-test PUBLIC gtest PUBLIC gmock)
The above lines will include both gtest and gmock dependency To check the gmock documentation clike here
git clone -b GMock_Extra_Credit --single-branch https://github.com/saimouli/PIDcontroller.git
cd <path to repository>
mkdir build
cd build
cmake ..
make
Run tests: ./test/cpp-test
Run program: ./app/PID-app
sudo apt-get install lcov
cmake -D COVERAGE=ON -D CMAKE_BUILD_TYPE=Debug ../
make
make code_coverage
In your Eclipse workspace directory (or create a new one), checkout the repo (and submodules)
mkdir -p ~/workspace
cd ~/workspace
git clone -b GMock_Extra_Credit --single-branch https://github.com/saimouli/PIDcontroller.git
In your work directory, use cmake to create an Eclipse project for an [out-of-source build] of cpp-boilerplate
cd ~/workspace
mkdir -p PID-eclipse
cd PID-eclipse
cmake -G "Eclipse CDT4 - Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug -D CMAKE_ECLIPSE_VERSION=4.7.0 -D CMAKE_CXX_COMPILER_ARG1=-std=c++14 ../PIDcontroller/
Open Eclipse, go to File -> Import -> General -> Existing Projects into Workspace -> Select "boilerplate-eclipse" directory created previously as root directory -> Finish
Source files may be edited under the "[Source Directory]" label in the Project Explorer.
To build the project, in Eclipse, unfold boilerplate-eclipse project in Project Explorer, unfold Build Targets, double click on "all" to build all projects.
-
In Eclipse, right click on the boilerplate-eclipse in Project Explorer, select Run As -> Local C/C++ Application
-
Choose the binaries to run (e.g. shell-app, cpp-test for unit testing)