如何选择特定的Google Mock测试用例/单元测试来运行?

How can you select specific Google Mock test cases/unit tests to run?

本文关键字:测试用例 Mock 单元测试 运行 Google 何选择 选择      更新时间:2023-10-16

我有多个单元测试,每个类在一个单独的文件中。

我的一个标准单元测试是这样的:

#include "gmock/gmock.h"
#include "gtest/gtest.h"
class ClassAUnitTest : public ::testing::Test {
protected:
    // Per-test-case set-up.
    // Called before the first test in this test case.
    // Can be omitted if not needed.
    static void SetUpTestCase() {
        //..
    }
    // Per-test-case tear-down.
    // Called after the last test in this test case.
    // Can be omitted if not needed.
    static void TearDownTestCase() {
        //..
    }
    // You can define per-test set-up and tear-down logic as usual.
    virtual void SetUp() {  }
    virtual void TearDown() {
}
    // Some expensive resource shared by all tests.
    //..
};
TEST_F(ClassAUnitTest, testCase1) {
    // Assign .. Act .. Assert.
}
我知道的方法是将DISABLED_放在测试用例的前面,像这样:
TEST_F(ClassAUnitTest, DISABLED_testCase1) {
    // Assign .. Act .. Assert.
}

但是,在处理一个失败的单元测试时运行所有测试是非常不切实际的。

我使用的是Visual Studio Ultimate 2013和Gmock 1.7.0。

问题:如何方便地选择运行哪些单元测试或特定测试,哪些不运行?

首先,你的单元测试应该是闪电般的快。否则人们不会执行它们。

如选择测试中所述,您可以使用--gtest_filter=选项。在您的具体情况下:--gtest_filter=ClassAUnitTest.*