【问题标题】:How to scan C++ code base and insert code in desired places?如何扫描 C++ 代码库并在所需位置插入代码?
【发布时间】:2022-01-07 17:14:22
【问题描述】:

我有兴趣扫描并以编程方式将一行代码插入代码库中的所有 googletest 测试用例,如下所示:

TEST(...) {
    RecordProperty("filename", __FILE__);
    ...
}
TEST_F(...) {
    RecordProperty("filename", __FILE__);
    ...
}
TEST_P(...) {
    RecordProperty("filename", __FILE__);
    ...
}

我有哪些选择?我想要实现的是在所有测试用例及其相应文件之间建立关联。除了上述方法之外,我也愿意接受其他方法。

【问题讨论】:

  • 您可以做 googletest 所做的事情并重新定义 TEST 宏。这就是那里发生的事情。
  • C++ 的面向方面编程利用简单令牌替换宏的力量。 (有笑/哭表情符号吗?这就是我想念 PostSharp 的原因。)

标签: c++ refactoring


【解决方案1】:

在这里回答我自己的问题——我采用的方法是像这样定义event listener

class CustomTestEventListener : public ::testing::EmptyTestEventListener {
    void OnTestEnd(const testing::TestInfo& testInfo) override {
        testing::Test::RecordProperty("key", testInfo.file());
    }
}

此侦听器将有效地在生成的 gtest xml 中添加一个部分,如下所示:

<testcase>
    ...
    <properties>
        <property name="key" value=""/>
    </properties>
</testcase>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多