简体   繁体   English

如何在C ++中为我自己的类编写gtest unittest

[英]How to write gtest unittest for my own classes in c++

I have defined my own class DoubleMatrix in C++. 我已经在C ++中定义了自己的类DoubleMatrix。 How can I write gtest unittests for it with different error messages, eg dimension mismatch or number of mismatches? 如何使用不同的错误消息(例如尺寸不匹配或不匹配数)为其编写gtest单元测试?

I need to realize smth like this code 我需要意识到这样的代码

for (int i = 0; i < x.size(); ++i) {
  EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
}

but it should be calling like this: 但它应该这样调用:

DoubleMatrix a, b;
EXPECT_EQ(a, b) 

or 要么

DoubleMatrix a, b;
double epsilon = 0.0001;
EXPECT_NEAR(a, b, epsilon)

You can define custom predicates to do the same. 您可以定义自定义谓词来执行相同的操作。

You can check https://github.com/google/googletest/blob/master/googletest/docs/advanced.md for details. 您可以查看https://github.com/google/googletest/blob/master/googletest/docs/advanced.md了解详细信息。 (check Predicate Assertions for Better Error Messages section in the link) (请检查链接中的谓词断言,以获得更好的错误消息

For example, you can have a function: 例如,您可以具有一个功能:

bool foo(DoubleMatrix a, DoubleMatrix b) {
// do the comparison and return true or false }

Use this via EXPECT_PRED2(foo, a, b); 通过EXPECT_PRED2(foo,a,b);使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM