googletest:https://github.com/google/googletest 注tag1.12.1是支持C++11的
mkdir build;cd build
cmake -DCMAKE_C_COMPILER=arm-linux-gnueabihf-sigmastar-9.1.0-gcc -DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-sigmastar-9.1.0-g++ …
make
lib目录下就是编译出来的静态库 libgmock.a libgmock_main.a libgtest.a libgtest_main.a
头文件路劲:…/googletest/include
如果链接了gtest_main,就不需要写main函数了,只要写TEST的测试用例(宏定义中不能有_)就好了,测试用例文件需要为cpp
参考链接:https://blog.csdn.net/weixin_39901032/article/details/127970251
gtest 支持一些参数输入设置:https://blog.csdn.net/King_weng/article/details/114289424,可以通过–help 查看
如: --gtest_repeat=-1 测试次数为无限, gtest_output=xml:./output.xml
–gtest_filter=testcase.json_post_get0 单例测试 TEST(testcase, json_post_get0)
TEST(para, json_www_get) para称为test suite, json_www_get称为test case,suite( 测试集合)可以包含多个case( 测试用例)
DISABLED:可以在测试集合或测试用例前面加,默认是不会跑的,用于一些已知会失败的,或者无限循环的例子
suite的setup方法:参考sample5_unittest,可以在suite开始前做相关的准备工作
定义class QuickTest : public testing::Test 以及TEST_F(QuickTest, json_www_get)
class ViscaTest : public testing::Test {
protected:
void SetUp() override {
//...setup work
}
void TearDown() override {
//tear down work
}
};
多线程测试:
void MyThreadFunction() {
}
TEST(MyMultiThreadTest, TestThreads) {
const int num_threads = 2;
address_init();
// 创建多个线程,并启动它们
std::vector<std::thread> threads;
for (int i = 0; i < num_threads; i++) {
threads.push_back(std::thread(MyThreadFunction));
}
// 等待每个线程完成
for (auto& thread : threads) {
thread.join();
}
}