Check if the function has been called in gtest

Harold

In gtest framework, is there any way to check whether a function has been called? (without gmock, use gtest only) for example:

class a
{
    public: 
    void dd() {...};
    void cc() {...};
    void bb() {...};
    void aa()
    {
        bb(cc(dd()));
    }
};

void main ()
{
    a dut;
    dut.aa();
}

I do not care the function input and even the correctness of the output. I just want to know if the function (e.g. aa()) has been triggered. Is there any solution? Many thanks in advance!

Tamás Szelei

without gmock, use gtest only

That's a very strict restriction. In general, you can't tell if a function was called. Gmock gets around this by generating mock functions that record the calls, arguments and can fake behavior based on runtime parameters.

Without this, you only have two options:

Observe a side effect of the function in question.

This is straightforward, but brittle: if you know there is a observable side effect of the function, you can check that:

class a
{
public: 
    a() : aa_flag(false) {}

    void aa()
    {
        aa_flag = true;
    }

    bool aa_flag;
};

TEST(FuncCalled, CheckSideEffectFlag)
{
    a dut;
    dut.aa();
    EXPECT_TRUE(dut.aa_flag);
}

You don't need to restrict yourself to flags that a function sets. Log messages and other side effects are also workable.

class a
{
public: 
    a() : aa_flag(false) {}

    void aa()
    {
        LOG_INFO("aa called");
    }

    bool aa_flag;
};

TEST(FuncCalled, CheckSideEffectLog)
{
    a dut;
    dut.aa();
    EXPECT_TRUE(LogContains("aa called"));
}

As mentioned above, this is a brittle solution, because you may be checking a side effect that randomly changes in the future. However, sometimes this is good enough.

Hotpatch the function to trace calls

This is nasty and I can't provide a complete example because the way to do this depends on your compiler and target. Basically, you instruct the compiler to generate the functions starting with a few no-op (not necessarily NOP) instructions. This allows you to take the address of the function, change these instructions to jump somewhere and then back. This is very useful because you can call a function that registers the return address and from that you can tell if a function was called or not. It was called iif the return address of aa() is registered.

You will need OS-specific calls to hotpatch your code and some knowledge of the CPU instructions you are running on. You obviously lose portability. I also don't know your requirements, but this probably isn't worth the trouble.


All in all, your best bet is gmock, if you want to stay in the boundaries of the standard. Virtual functions are the standard way of dynamic dispatch (which is what you would do if you were hotpatching your image).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I check if an anonymous function has been called with NSubstitute?

From Dev

Elixir check if a function has been called from a ExUnit test?

From Dev

Check the conditions every time after function has been called

From Dev

sinon spy check if function has been called error

From Dev

Scheme: How to check if a function has been called with a same argument

From Dev

How to check if a certain method has been called?

From Dev

Nsubstitute check if setter has been called

From Dev

Check if redux action has been called with enzyme

From Dev

How to check how many times a recursive function has been called in Java?

From Dev

What is the canonical way to check if a function has been called in Python unittest without use of a mock?

From Dev

How to check how many times a recursive function has been called in Java?

From Dev

How to determine if a function has been called in Powershell

From Dev

A function to check if a file has been changed

From Dev

Are guards to check if a method has been called optimized by Java?

From Dev

Perl: How to check if CGI::header has already been called?

From Dev

Check in PHP if date_default_timezone_set() has been called

From Dev

Is it impossible to tell if a function is a generator function if .bind() has been called on it?

From Dev

How to test that a function has been called after an event was fired?

From Dev

Checking whether function has been called multiple times with different parameters

From Java

How can I test that a function has not been called?

From Dev

How to test how many times a function has been called

From Dev

Abort() has been called - Connect Function multithreads Cpp

From Dev

Counting how many times a function has been called recursivly in JS

From Dev

Tkinter: 'after" function is still called after root has been destroyed

From Dev

How to test how many times a function has been called

From Dev

what will remain of scope of a function after it has been called?

From Dev

Get absolute path for the file where the function has been called

From Dev

AngularJS unit test check if function without methods have been called

From Dev

How to make sure a class function wont be called until another class function has been called first?

Related Related

  1. 1

    How can I check if an anonymous function has been called with NSubstitute?

  2. 2

    Elixir check if a function has been called from a ExUnit test?

  3. 3

    Check the conditions every time after function has been called

  4. 4

    sinon spy check if function has been called error

  5. 5

    Scheme: How to check if a function has been called with a same argument

  6. 6

    How to check if a certain method has been called?

  7. 7

    Nsubstitute check if setter has been called

  8. 8

    Check if redux action has been called with enzyme

  9. 9

    How to check how many times a recursive function has been called in Java?

  10. 10

    What is the canonical way to check if a function has been called in Python unittest without use of a mock?

  11. 11

    How to check how many times a recursive function has been called in Java?

  12. 12

    How to determine if a function has been called in Powershell

  13. 13

    A function to check if a file has been changed

  14. 14

    Are guards to check if a method has been called optimized by Java?

  15. 15

    Perl: How to check if CGI::header has already been called?

  16. 16

    Check in PHP if date_default_timezone_set() has been called

  17. 17

    Is it impossible to tell if a function is a generator function if .bind() has been called on it?

  18. 18

    How to test that a function has been called after an event was fired?

  19. 19

    Checking whether function has been called multiple times with different parameters

  20. 20

    How can I test that a function has not been called?

  21. 21

    How to test how many times a function has been called

  22. 22

    Abort() has been called - Connect Function multithreads Cpp

  23. 23

    Counting how many times a function has been called recursivly in JS

  24. 24

    Tkinter: 'after" function is still called after root has been destroyed

  25. 25

    How to test how many times a function has been called

  26. 26

    what will remain of scope of a function after it has been called?

  27. 27

    Get absolute path for the file where the function has been called

  28. 28

    AngularJS unit test check if function without methods have been called

  29. 29

    How to make sure a class function wont be called until another class function has been called first?

HotTag

Archive