如何自定义Google测试失败消息?

2022-08-26 00:00:00 testing unit-testing c++ googletest

我已经编写了一个类似下面的Google测试,将一些计算值与CSV文件中存储的预期值进行比较。

class SampleTest : public ::testing::Test{
public:

    void setupFile(const std::string& filename) {
       // open csv file here
    }

    void checkRow(ComputedRowValue val) {
        CSVParsedOutput out;
        m_csv_f.readAndParseLine(out);
        EXPECT_EQ(out.field1, val.field1);
        EXPECT_EQ(out.field2, val.field2);
        EXPECT_EQ(out.field3, val.field3);
        m_csv_line++;
    }


protected:
    CSVFile m_csv_f; // CSV file with expected results
    int m_csv_line = 0;
};

这将运行一些巨大的文件大小,失败时Expect_EQ只会告诉我哪些值不匹配。如何覆盖Expect_EQ输出的错误消息以同时打印m_csv_line


解决方案

您可以将Expect_EQ用作流,以便: EXPECT_EQ(out.field1, val.field1) << m_csv_line; 应该做你想做的事。

相关文章