gtest 学习之五 测试用例中定义类
上一个例子中被测试的类是作为测试类的一个成员变量,在本例中没有测试类,被测试类只能在一个测试用例中定义使用
头文件sample4.h
#ifndef GTEST_SAMPLES_SAMPLE4_H_ #define GTEST_SAMPLES_SAMPLE4_H_ // A simple monotonic counter. class Counter { private: int counter_; public: // Creates a counter that starts at 0. Counter() : counter_(0) {} // Returns the current counter value, and increments it. int Increment(); // Prints the current counter value to STDOUT. void Print() const; }; #endif // GTEST_SAMPLES_SAMPLE4_H_
main.cpp:
#include "gtest/gtest.h" #include "sample4.h" TEST(Counter, Increment) { Counter c;//在测试用例中定义变量 // EXPECT_EQ() evaluates its arguments exactly once, so they // can have side effects. EXPECT_EQ(0, c.Increment()); EXPECT_EQ(1, c.Increment()); EXPECT_EQ(2, c.Increment()); } int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
运行结果: