'How to test if output function recieved is are correct or not using GTEST/GMOCK

I am trying implemnent the GTEST/GMOCK framework for unittesting the C-code. I am using the MSVS 2017. The adaptation is completed.

Here, I have the matlab generated code. This autogenerated code we have simulating in the PC level which has the .Net GUI for giving the inputs (This GUI is now will be replaced by GTEST framework from which we will be invoking the input functions). Whenever I call any function (Give any input) we will confirming the functionality by checking the logs.

The C functions we are invoking are all void functions but there are separate(different) output function (are also void type) from which we will be confirming the functionality.

Now I need to implement the unit testing is the same way. On calling the input function (Which are void type) I need to verify that if the output functions (void type) that received are correct or not. Here we don’t have any return values. Is there any way to implement this idea?Currently the EXPECT_CALL is actually checking if the function call is there or not even if I pass wrong argument the test says passed. So I am stuck here.

The implemetation is as below :

/*
============================================================================
 Name        : tesp.cpp
Author      : Siddhartha V
 ============================================================================
 */

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <iostream>

#include "pch.h"

class Testfuncs {
 public:
  virtual ~Testfuncs() {}
  virtual voidoutput_Func1(RsuCtrl_t rtu_Id, HuStat_t rtu_RsuStatSeat);
  virtual void output_Func2() = 0;
  virtual void Input_Func1(int Stat) = 0;
  virtual void Input_Func2(int Stat) = 0;
};

class MockTestfuncs : public Testfuncs {
 public:
  MockTestfuncs() : Testfuncs() {}
  MOCK_METHOD(void, output_Func1, (RsuCtrl_t rtu_Id, HuStat_t rtu_RsuStatSeat),
              (override));
  MOCK_METHOD(void, output_Func2, (), (override));
  MOCK_METHOD(void, Input_Func1, (intStat), (override));
  MOCK_METHOD(void, Input_Func2, (int Stat), (override));
};

TEST(TestVC, Read_UpdateInProgress) {
  std::chrono_literals;
  m_VInput.ECall_Active = 1;
  SetInputs(&m_Input);
  EXPECT_FALSE(0, output_func_n(void));
  std::this_thread::sleep_for(9s);
}

TEST(MockTestfuncs, output_Func1) {
  MockTestfuncs Testfuncs;
  std::this_thread::sleep_for(5s);
  //***setting the input @@@@@
  Input_Func2((int)1);
  using ::testing::_;
  using ::testing::AllOf;
  using ::testing::Args;
  using ::testing::Lt;
  EXPECT_CALL(Testfuncs, Input_Func1((int)1));
  Testfuncs.Input_Func1((int)1);
  EXPECT_CALL(Testfuncs, Input_Func2((int)1));
  Testfuncs.Input_Func2((int)1);
  EXPECT_CALL(Testfuncs, output_Func1).Times(1);
  Testfuncs.output_Func1((RsuCtrl_t)1, (HuStat_t)2);
  ASSERT_TRUE(100, Input_Func2((int)1));
  std::this_thread::sleep_for(1s);
}

TEST(TestVC, RSUF_testEXP_TRUE) { EXPECT_FALSE(1, Input_Func1((int)1)); }

TEST(TestVC, RSUF_testEXP_EQ) { EXPECT_TRUE(1, Input_Func1((int)1)); }

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  std::cout << "Gtest main is printing";
  return RUN_ALL_TESTS();
}

Any suggestion will help me a lot.

regards, Siddhartha V



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source