Advertisement
193030

02. Google test Framework Test Fixture TEST_F

Dec 14th, 2021
1,319
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None
  1. #include "pch.h"
  2. #include <math.h>
  3. #include "gtest/gtest.h"
  4.  
  5. // Unit test
  6. // 1. The unit test must run extremely fast (within Milliseconds)
  7. // 2. Must be able to run independently
  8. // 3. Doesn't depend upon any external input
  9. using namespace std;
  10. class MyClass
  11. {
  12. public:
  13.     int value;
  14.     MyClass(int value)
  15.     {
  16.         this->value = value;
  17.     }
  18.     void Increment()
  19.     {
  20.         value += 5;
  21.     }
  22.     int getValue()
  23.     {
  24.         return value;
  25.     }
  26.    
  27. };
  28.  
  29. struct MyClassTest : public testing::Test
  30. {
  31.     MyClass* mc;
  32.     void SetUp()
  33.     {
  34.         cout << " alive" << endl;
  35.         mc = new MyClass(100);
  36.     }
  37.     void TearDown()
  38.     {
  39.         cout << "Tear down" << endl;
  40.         delete mc;
  41.     }
  42. };
  43.  
  44. TEST_F(MyClassTest, TextFixture_test1)
  45. {
  46.     mc->Increment();
  47.     ASSERT_EQ(mc->getValue(), 105);
  48. }
  49.  
  50. TEST(TestName, incremented_by_10)
  51. {
  52.     int value = 100;
  53.     int increment = 10;
  54.     value = value + increment;
  55.  
  56.     ASSERT_EQ(value, 110);
  57. }
Advertisement
RAW Paste Data Copied
Advertisement