I am using gtest/gmock framework to write unit tests. Following is my sample code.
In common.h,
void PreInit() {
cout<<"Doing Pre-initialization."<<endl;
}
In test.cpp,
#include <common.h>
class SampleComponent {
public:
void Init() {
PreInit();
// Do Initialization
}
}
So far, I have been mocking classes. Any idea how to mock independent functions called by member functions of class?
Thanks in advance.
Related
So I am trying to write test cases for my production code but the coverage is drastically low due to the usage of some external C library which cannot be executed without target hardware, So I have no choice but to stub the same. Now the problem is how to stub a C function ?
My production code : prod_code.cpp
int TargetTestClass::targetFunc()
{
if(externalCFunc() == True)
{
statement1; statement2; statement3; /// and so on
}
}
My testcode.cpp generally contains tests like this
//Fixture for Target Test class
class TargetTestClassFixture : public testing::Test {
TargetTestClass* targetTestClassPtr;
void SetUp() {
targetTestClassPtr = new TargetTestClass();
}
void TearDown() {
delete targetTestClassPtr;
}
};
TEST_F (unitTest, test_001)
{
targetTestClassPtr->targetFunc(); //Need to do something here so that externalCFunc() returns true on call
}
What you can do is to create a source file like my_c_stubs.c where you rudimentary implement your C function. For example, the implementation can just return true. Then don't link original source file with the external C function but rather use your stub file. You should still use the original C header. In this way you won't be able to stub inline functions though. If it is required, some more sophisticated approach is needed.
I found 2 solutions to my problem so I am going to answer the same here.
Solution 1 : This involved changing the target source code. Basically you need to write a wrapper that calls the external C functions like below
Class TargetTestClass{
protected:
int targetFunc();
virtual int externalCFuncWrapper(); // Wrapper
};
//call the external C function from the wrapper
int TargetTestClass::externalCFunctionWrapper(){
return(externalCFunc());
}
//Definition of targetFuc in original question
//Now write a mock class for Target Test Class as usual and mock the wrapper function to return what you want to
class MockTargetTestClass : public TargetTestClass{
public: MOCK_METHOD0(externalCFunctionWrapper, int());
};
//Now use the Mock class as needed
TEST_F ( TargetUnitTest, TestingExternalCFuctionCall)
{
MockTargetTestClass mockTargetTestClassObj;
using ::testing::Return;
using ::testing::_;
Expect_Call(mockTargetTestClassObj, externalCFunctionWrapper())
.WillOnce(Return(1));
Assert_EQ(mockTargetTestClassObj.targetFunc(), 1);
}
Solution 2 : Thanks to #kreynolds, I have looked into Fake Function Framework and implemented as follows :
Class TargetTestClass{
protected:
int targetFunc();
//No Code change needed in target source code
};
//In testcode.cpp
#include <gmock-global/gmock-global.h>
MOCK_GLOBAL_FUNC0(externalCFunc, int());
TEST( Unittest, test002){
using ::testing::Return;
using ::testing::_;
EXPECT_GLOBAL_CALL(externalCFunc, externalCFunc()).WillOnce(Return(1));
TargetTestClass targetFunc; //This does not contain any wrapper
EXPECT_EQ(targetTestClassObj.targetFunc(), 1);
}
I am using the second solution as this does not require any change in my source code and easier to use.
Once again thank you everyone for giving your time.
Intro
I'm having a problem with implementing a pure virtual interface using Google Mock (v1.7.0).
In order to determine the root cause, I've put together pure virtual interface Simple and MockSimple as a test. I believe what I have done here is in line with this Google Mock example.
Yet, when I compile it, I get an error. Why is this? What am I doing wrong?
Simple Test Code
#include "gmock/gmock.h"
#include "gtest/gtest.h"
class Simple
{
public:
virtual ~Simple() {}
virtual int Initialize() = 0;
};
class MockSimple : public Simple
{
public:
MOCK_METHOD0(Initialize, int());
};
TEST(Hello, MockSimple)
{
MockSimple m;
EXPECT_CALL(m, Initialize);
m.Initialize();
}
Compilation Command
g++ -I../gmock/include/ -I../gmock/gtest/include -c test.cpp -o test.o
Error Output
test.cpp: In member function ‘virtual void Hello_MockSimple_Test::TestBody()’:
test.cpp:20:5: error: ‘m.MockSimple::gmock_Initialize’ does not have class type
Need argument list (an empty one, in this case) for the method called in the EXPECT_CALL :)
EXPECT_CALL(m, Initialize());
I'm migrating from C++ to SystemC, met following basic questions. (I've searched in google, but only got examples in a single .cpp file). Thanks in advance.
I know following "hello.cpp" works:
//hello.cpp
#include "systemc.h"
SC_MODULE (hello_world) {
SC_CTOR (hello_world) {
}
void say_hello() {
cout << "Hello World.\n";
}
};
int sc_main(int argc, char* argv[]) {
hello_world hello("HELLO");
hello.say_hello();
return(0);
}
Question 1: How can I separate it into hello.h and hello.cpp? Following code is in C++, I don't know how to create the equivalent SystemC code.
//hello.h
class hello_world
{
public:
hello_world();
void say_hello();
};
//hello.cpp
hello_world::hello_world()
{
}
hello_world::say_hello()
{
cout << "Hello World.\n";
}
Question 2: How to create nested class in SystemC? E.g. what is the equivalent SystemC code according to following C++?
class foo
{
public:
int fooAttr1;
class bar
{
public:
int barAttr1;
};
};
Question 3: Where is the best place to specify an attribute/operation's scope? (public/protected/private).
When separating out the implementation from the declarations in SystemC, you can do it the normal way as it is done in C++.
But when you want a constructor with more than one argument(except the default one i.e. SC_CTOR accepts the module name) you will have to define your own constructor.
And if the module has SystemC processes(SC_THREAD, SC_METHOD, SC_CTHREAD) then you will have to use the SC_HAS_PROCESS macro to indicate that your module has process/es.
And regarding nested class it is the same thing as in C++.
I am not sure what you mean by the 3rd question.
regarding your 3rd question: you can do same as you usually do in a C++ program. The best place to specify an attribute/operation's scope is the declaration part of the SC_MODULE, whether you put it in cpp or header file.
In a SystemC design, normally only the ports and constructor/destructor should be defined as public to allow other modules to connect with it, unless you have a strong reason and want to break the modularity of a systemc design to access explicitly the member functions of a module.
I'm implementing a test class for my project in QT. I have added tests for all the public functions, however there are a couple of private functions that id like to include in my testing.
Obviously this wont work because they are private member functions, but i wondered if this is able to be bypassed on the basis i want to access them from a test case, i dont particularly want to change my code so that my tests work because thats not really helpful, id like the functions to be private but i also want to be able to test them.
Just wondering if anyone has any ideas on whether this is possible or if i need to change my functions to protected or something and then inherit the class for my test?
It is common practice to only unit test public functions but you can make the test class as the friend of the original class. Something like that:
The decleration of friend should be inside a #define UNIT_TEST flag.
#include <iostream>
class ToTestClass
{
#ifdef UNIT_TEST
// ToTestClass declares TesterClass as a friend.
friend class TesterClass;
#endif
private:
void privateMethod()
{
std::cout << "hey there" << std::endl;
}
};
class TesterClass
{
public:
TesterClass()
{
ToTestClass totest;
// TesterClass now has access to ToTestClass's private members and methods.
totest.privateMethod();
}
};
int main()
{
TesterClass tester;
return 0;
}
Option one. Your functions may be conditionally public private
#ifdef UNIT_TEST
#define ut public
#else
#define ut private
class Testee
{
public:
....
ut:
//private functions to be tested
ptivate:
};
Then, in your test header, simply
#define UNIT_TEST
#include <Testee.h>
This is a bit dangerous if the tests and the application are in the same project (program) because according to the standard the class definitions must be the same in all translation units in a same program, otherwise, undefined behavior.
Second, and easier and safer option is to declare the Tester class as a friend.
class Testee
{
public:
....
ptivate:
....
friend class Tester;
};
I try to use MSTest so as to perform unit tests on native C++ code (so as to try to implement Test Driven Development).
There's an MSTest entry in the "Add New Project" C++ Wizard. It creates some code obviously in C+++/CLI.
However, whenever I try to run these tests, Visual Studio indicates me that the tests are not runnable:
Not Runnable TestMethod2 CargoOCRTests UTA007: Method TestMethod2 defined in class CargoOCRTests.UnitTest1 does not have correct signature. Test method marked with the [TestMethod] attribute must be non-static, public, does not return a value and should not take any parameter. for example: public void Test.Class1.Test().
However I think my two tests functions do respect the prototype VS is complaining about:
namespace CargoOCRTests
{
[TestClass]
public ref class UnitTest1
{
[TestMethod]
void TestMethod1()
{
Assert::IsTrue(true);
};
[TestMethod]
void TestMethod2()
{
Assert::IsTrue(false);
};
};
}
Have you any idea what is the cause ?
You need to mark your test methods as public
public:
[TestMethod]
void TestMethod1() {}