C/C++ 测试

模板测试
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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值