Calling one TEST_F in another TEST_F in gtest - c++

I have a test suite in gtest.My Test Fixture class has two tests,one is Test_F(functional),Test_F(performance).
I have the implementation in such a way that Test_F(performance) is dependent on the run of Test_F(functional).
I want to handle the case,where when Test_F(functional) is disabled.So,my approach is calling Test_F(functional) from inside Test_F(performance).
I am not sure,how this is done in gtest,calling a test_function,inside another test_function.
Any help is appreciated.

AFAIK there's no option to call test function inside another test function. However, to share the code between test cases, you can define a helper method in the test suite:
class FooBarTestSuite : public ::testing::Test {
public:
TestSuite() {
// init code goes here
// foo and bar are accessible here
}
~TestSuite() {
// deinit code goes here
// foo and bar are accessible here
}
void CommonCode() {
// common test case code here
// foo and bar are accessible here
}
ClassFoo foo{};
ClassBar bar{};
};
TEST_F(FooBarTestSuite, PerformanceTest) {
CommonCode();
// performance-specific code here
}
TEST_F(FooBarTestSuite, FunctionalTest) {
CommonCode();
// functional-specific code here
}
In order to run a specific test case, use --gtest_filter=*Performace* or --gtest_filter=*Functional* as the parameter.

Related

What is the use of a test fixture in google test?

Google recommends to use the text fixture constructor/destructor when possible instead of SetUp()/TearDown() (https://google.github.io/googletest/faq.html#CtorVsSetUp). Assuming I do it this way, what is the use of even using a test fixture? How are the following different, and what is the advantage of the first?
TEST_F(MyFixture, MyTest) {
... run test, using member functions of MyFixture for common code ...
}
TEST(MySuite, MyTest) {
MyFixture fixture; // will call ctor
... run test, using public functions of MyFixture for common code ...
} // will call dtor
The advantages are visible when there are more than one TEST/TEST_F.
Compare:
TEST(MyTest, shallX)
{
MyTest test;
test.setUpX();
test.objectUnderTest.doX();
}
TEST(MyTest, shallY)
{
OtherTest test;
test.setUpY();
test.objectUnderTest.doY();
}
with
TEST_F(MyTest, shallX)
{
setUpX();
objectUnderTest.doX();
}
TEST_F(MyTest, shallY)
{
setUpY();
objectUnderTest.doY();
}
What we can see, are:
DRY (don't repeat yourselves) principle is followed. You do not have to repeat creating of some test-helper object. In TEST_F - the macro creates this instance.
The code is safer with TEST_F. See MyTest..shallDoY -- have you spot that wrong test-helper object is used, not the one that testname is promising.
So it is better to use TEST_F if your tests require some test-helper class.
If not - then use TEST.

How to write a test case for module dependent function using EXPECT_CALL in Google test?

I am new to google test environment. I have a sample code written in C and want to perform unit test with Google test framework.
Below is the sample code
// My module function (test.c)
void Update_user_value(void)
{
int user_value;
user_value = get_val_module(); /* return a integer value*/
if(user_value == 0x1)
update_user_flag(true);
else
update_user_flag(false);
}
// This function is in the other module(stub.c) , so we can use Mock function
void update_user_flag(bool val)
{
struct *temp;
if(val == true)
{
temp->userflag = 1;
}
else
{
temp->userflag = 0;
}
}
I wan to write a test case for the Update_user_value function (only for test.c). Through this function, i am sending some flag value to other module (update_user_flag) to set the flag.
I have written a simple google test like this
TEST_F(sampletest, setuser_flag_true_testcase)
{
//Get value from module
ON_CALL(*ptr1, get_val_module()).WillByDefault(Return(0x1)); //Mock function
EXPECT_CALL(*ptr2, get_val_module(_)).Times(1); // Mock function
Update_user_value();
}
TEST_F(sampletest, setuser_flag_false_testcase)
{
//Get value from module
ON_CALL(*ptr1, get_val_module()).WillByDefault(Return(0x0)); //Mock function
EXPECT_CALL(*ptr2, get_val_module(_)).Times(1); // Mock function
Update_user_value();
}
My question: Is this test case is enough to validate the Update_user_value function ?
Also i want to know, EXPECT_CALL() is good to use for setting a value to other module ?
If my understanding is wrong, please suggest me a better test case ?
ON_CALL, EXPECT_CALL are macros designed to be used on mock objects. Usually the use case is as follows:
You create an interface to derive from (it will be mocked in your test).
You pass the mock object (to method or via dependency injection).
You make expectations on this object.
See example:
class Foo {
public:
virtual ~Foo() = default;
virtual int bar() = 0;
};
class FooMock : public Foo {
public:
MOCK_METHOD0(bar, int());
};
bool check_bar_over_42(Foo& foo) {
if (foo.bar() > 42) {
return true;
}
return false;
}
TEST(check_bar_over_42_test, bar_below_42) {
FooMock fooMock{};
EXPECT_CALL(fooMock, bar()).WillOnce(testing::Return(41));
ASSERT_FALSE(check_bar_over_42(fooMock));
}
TEST(check_bar_over_42_test, bar_above_42) {
FooMock fooMock{};
EXPECT_CALL(fooMock, bar()).WillOnce(testing::Return(43));
ASSERT_TRUE(check_bar_over_42(fooMock));
}
AFAIK there is no way of using EXPECT_CALLs on C-like functions. One approach for your problem would be link-time mocking: given a method get_val_module is defined in a separate library, you can create test-only library with get_val_module that will allow you to return the expected values. In tests you would link against test lib, in production - against the real lib.

Can google mock mock the behavior of changing the out param?

I start using gmock for my code recently, some of which rely on some other outside services or interfaces, so gmock is kind of perfect for me.
Say I have a trivial class A to mock
class A
{
void foo(int& bar) {
// will change bar
bar = GetingResultFromSomeOutsideService();
}
};
Now I create a mock class
class MockA: public A
{
MOCK_METHOD1(foo, void(int& bar));
};
In my test case, I try to mock the behavior of A::foo of changing bar with ON_CALL, but I don't know how exactly.
For example, I want my MockA::foo always set bar to 1, so that my other codes invoking A::foo will always get bar == 1 like this:
// some other piece of codes invoking A::foo
int bar = 0;
A my_a;
my_a.foo(bar);
cout << bar; // always output "1" with gmock
Any hint?

How does one verify state for a mock object?

I have a program Foo that runs on a device and first calls method1() and then method2() on an external module Bar.
When both are called, then in response, Bar calls request() on Foo.
To mock Bar, it has a reference to Foo and a method call_Request() which calls foo.request()
// in class BarTest
#Test
public void barFlow() {
InOrder inOrder = inOrder(mockBar);
inOrder.verify(mockBar).method1(anyInt());
inOrder.verify(mockBar).method2();
// what to put as 'thenCallMethod'?
when(mockBar.method2()).thenCallMethod().call_Request();
}
...
// in class Bar
public void call_Request() {
foo.request();
}
I cannot simply put a call to call_Request() in method2 because only when both methods are called in order does the response happen. Other times, different responses may happen.
what Mockito trick will do 'thenCallMethod'?
(I am using the Mockito ver 1.10.19 bundled with Android Studio 2.1)
You seem to have no clear definition of what your unit under test is. There seem to be 3 possiblities:
there are separate tests for Foo and Bar and the unit under test is an instance of Foo using a mock of Bar and an instance of Bar using a mock of Foo
the unit under test is an instance of Foo using a real instance of Bar, but the Bar instance uses a Foo mock
the unit under test is an instance of Foo using a real instance of Bar which in turn uses the existing instance of Foo
In case 1. you could test the following for Bar:
#Test
public void testBar1() {
Foo mockFoo = Mockito.mock(Foo.class);
Bar bar = //obtain unit under test that uses mockFoo
bar.method1(1/*or whatever int value is appropriate*/);
bar.method2();
Mockito.verify(mockFoo).request();
}
#Test
public void testBar1() {
Foo mockFoo = Mockito.mock(Foo.class);
Bar bar = //obtain unit under test that uses mockFoo
bar.method2();
bar.method1(1/*or whatever int value is appropriate*/);
Mockito.verify(mockFoo, Mockito.never()).request();
}
And the test for Foo would look like:
Bar mockBar = Mockito.mock(Bar.class);
Foo foo = //obtain unit under test that uses mockBar
// invoke method on foo that would trigger calls to mockBar
InOrder inOrder = inOrder(mockBar);
inOrder.verify(mockBar).method1(anyInt());
inOrder.verify(mockBar).method2();
In case 2. you would basically repeat the test for Foo from case 1. But you would just verify that Foo.request() was invoked on the mock of Foo used by the instance of Bar. The order of invocations on the Bar instance are not of any interest for this test case.
In case 3. you wouldn't be able to verify any invocations, because there are no mocks used at all.
Using something like
when(mockBar.method2()).thenCallMethod().call_Request();
doesn't make much sense, because you give a mock an implementation, that it might not have in production code.

Stub a method from tested class to test another method

I discovered that stub and mock are very helpful in testing.
But I wondering about one thing. I think an example will show it clearly.
Class A {
public function isOk() {
// some work
}
public function iAmDepend() {
if ($this->isOk()) {
// do work
}
}
}
class TestA {
public function testIsOk() {
// Test itOk here
}
public function testIAmDepend() {
mock(A)->method(isOk)->return(true);
// tests for iAmDepend
}
}
It wise to do something like this? Stub method of tested class. Or maybe it breaks some rules?
PS. I can't refactore code
Your examples are correct, i.e. testIsOk tests only IsOk, and testIAmDepend only IAmDepend.
But there is important difference between mock and stub that you have to understand: difference between mock and stub.
In your example, if testIAmDepend is verifying that isOk has been called with some arguments and this is part of your assertion for unittest, this is mock. Otherwise this is stub, and you aren't going to verify that it has been called or not.