zoukankan      html  css  js  c++  java
  • GoogleTest死亡测试的跨平台BUG

    最近工作用到了GoogleTest来作单元测试,但是死亡测试的ASSERT_DEATH语句一直跑不通。

    GoogleTest会启动子进程来运行代码,并捕捉子进程的错误消息,这就是所谓的“死亡测试”。

    直接上代码:

     1 #include <stdio.h>
     2 #include <iostream>
     3 
     4 #include <gtest/gtest.h>
     5 
     6 using namespace std;
     7 
     8 class X {
     9 public:
    10     X() {}
    11 
    12     ~X() {
    13         this->print();
    14     }
    15 
    16 private:
    17     void print() {
    18         std::cerr << "haha";
    19         abort();
    20     }
    21 
    22 };
    23 
    24 class Y {
    25 public:
    26     Y() {}
    27 
    28     ~Y() {
    29         this->print();
    30     }
    31 
    32 private:
    33     void print() {
    34         fprintf(stderr, "haha");
    35         abort();
    36     }
    37 
    38 };
    39 
    40 TEST(FooTest, DoesXyz) {
    41     EXPECT_DEATH(X(), "ah");
    42     EXPECT_DEATH(Y(), "ah");
    43 }
    44 
    45 int main(int argc, char *argv[]) {
    46     ::testing::InitGoogleTest(&argc, argv);
    47     return RUN_ALL_TESTS();
    48 }

    上述代码,在Linux GCC编译后全部pass;但在Windows VC编译后,只有X的测试用例跑通,Y的失败。

    具体原因暂时不清楚,因此在Windows VC开发环境下,推荐使用std:cerr来输出错误信息。否则用stderr的话,GoogleTest会无法捕捉到消息。

  • 相关阅读:
    数据结构
    查找算法
    排序算法
    ThreadPoolExecutor
    Python map()函数
    Python惰性序列
    Python iterator迭代器
    Python yield关键字 和 Generator(生成器)
    Python 列表生成式(List Comprehensions)
    Python 迭代(iteration)
  • 原文地址:https://www.cnblogs.com/shishaochen/p/5708516.html
Copyright © 2011-2022 走看看