Click here to Skip to main content
15,397,729 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written some unit test cases using Gtest framework. If I am trying to test the class whose constructor is making the object of another class, GTest is crashing.

Code is as follows:

User1.cpp
class User1
{
    public:
           User1()
           {
                u = new User2();  //Line exp
           }
           ~User1()
           {
                delete u;
           }
    private:
          User2* u;
}

User2.cpp
class User2
{
     public:
           User2(){}
           ~User2(){}
}

Test Case:
TEST(test, InstantiateUser1)
{
     User1* u = new User1();
}

When I did debugging the test case, it was crashing at line exp (mentioned in comment).
Please help me out why is it happening and How can I resolve this problem?

What I have tried:

I searched a lot but not got anything relatable. I have no clue to solve this issue.
Posted
Updated 23-Jun-20 2:35am
Comments
ZurdoDev 22-Jun-20 12:14pm
   
What is the error?
Richard MacCutchan 22-Jun-20 12:27pm
   
The debugger should show you why it crashes.
Member 14686752 23-Jun-20 0:15am
   
Exception thrown at 0x00000000000ACDB6 in Unittests.exe: 0xC0000005: Access violation executing location 0x00000000000ACDB6
Member 14686752 23-Jun-20 0:16am
   
Same code in my project is working without any error but in gtest it is crashing.
Richard MacCutchan 23-Jun-20 2:46am
   
That is a problem with Gtest.
Member 14686752 23-Jun-20 2:52am
   
is there any way to resolve this?
Richard MacCutchan 23-Jun-20 2:57am
   
Does your code run correctly outside of Gtest? If it does, then the problem is most likely in the test framework, so you need to try their helpline.
Member 14686752 23-Jun-20 3:21am
   
Yeah it is working correctly outside of Gtest.
Richard MacCutchan 23-Jun-20 3:31am
   
I have not used Gtest, but it is possible that your test is not written correctly. Check the documentation.

1 solution

I have been looking at the Google Test documentation, and I think your test code is not correct. The test needs to call the function to be tested, and verify the results. See googletest/primer.md at master · google/googletest · GitHub[^] for the correct way to write tests.
   
Comments
Member 14686752 25-Jun-20 2:33am
   
Test case is written in correct way, still not working.
Richard MacCutchan 25-Jun-20 4:24am
   
Once again, that tells us nothing. Please edit your question and show exactly what you have done. We most likely also need to see the missing code from your samples.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900