模板测试
test_01.cpp
#include <gtest/gtest.h>
#include <stdio.h>
using testing::Types;
template <class T>
class TypeTest : public testing::Test {
public:
bool CheckData() {
if (typeid(T) == typeid(bool)) {
return false;
}
else {
return true;
}
}
};
typedef Types<int,long> IntegerTypes;
TYPED_TEST_CASE(TypeTest,IntegerTypes);
TYPED_TEST(TypeTest,Verify){
EXPECT_TRUE(CheckData());
printf("%s\n","success");
};
GTEST_API_ int main(int argc, char **argv)
{
printf("Running main() from gtest_main.cc\n");
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
g++ test_01.cpp -lgtest -lstdc++ -lpthread -fpermissive
Running main() from gtest_main.cc
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from TypeTest/0, where TypeParam = int
[ RUN ] TypeTest/0.Verify
success
[ OK ] TypeTest/0.Verify (0 ms)
[----------] 1 test from TypeTest/0 (0 ms total)
[----------] 1 test from TypeTest/1, where TypeParam = long
[ RUN ] TypeTest/1.Verify
success
[ OK ] TypeTest/1.Verify (0 ms)
[----------] 1 test from TypeTest/1 (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (0 ms total)
[ PASSED ] 2 tests.
类测试
test_02.cpp
#include<iostream>
#include<string>
#include"main.h"
using namespace std;
CBox::CBox(double lv, double wv, double hv)
{
cout << "Constructor called." << endl;
m_Length = lv;
m_Width = wv;
m_Height = hv;
}
CBox::CBox()
{
cout << "Default constructor called." << endl;
}
double CBox::volume()
{
return m_Length* m_Width* m_Height;
}
test_02.h
#include<iostream>
#include <string.h>
using namespace std;
class CBox{
public:
double m_Length;
double m_Width;
double m_Height;
CBox(double lv, double wv, double hv);
CBox();
double volume();
};
test_main.cpp
#include <iostream>
#include "gtest/gtest.h"
#include "main.h"
using namespace std;
class CBox_test:public testing::Test {
protected:
CBox* c;
virtual void SetUp()
{
c= new CBox(2, 3, 4);
}
virtual void TearDown()
{
delete this->c;
}
};
TEST(CBox, case1){
CBox box1(78.0, 24.0, 18.0);
CBox box2;
EXPECT_LT(23.0, box1.volume());
}
TEST_F(CBox_test, case2){
CBox box1(78.0, 24.0, 18.0);
CBox box2;
EXPECT_LT(23.0, box1.volume());
EXPECT_EQ(24,c->volume());
}
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
g++ -o mm test_main.cpp test_02.cpp -lstdc++ -lgtest -lpthread
./mm
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from CBox
[ RUN ] CBox.case1
Constructor called.
Default constructor called.
[ OK ] CBox.case1 (0 ms)
[----------] 1 test from CBox (0 ms total)
[----------] 1 test from CBox_test
[ RUN ] CBox_test.case2
Constructor called.
Constructor called.
Default constructor called.
[ OK ] CBox_test.case2 (0 ms)
[----------] 1 test from CBox_test (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (0 ms total)
[ PASSED ] 2 tests.