我尝试使用谷歌测试框架,但不明白为什么出现以下代码:
TEST(MathTest, BelowZeroCandF)
{
EXPECT_DOUBLE_EQ(convertCtoF(-1), 30.2);
}
Run Code Online (Sandbox Code Playgroud)
在哪里
double convertCtoF(double c)
{
return 32+1.8*c;
}
Run Code Online (Sandbox Code Playgroud)
失败:
Failure
Value of: -30.20
Actual: -30.199999999999999
Expected: tc.convertCtoF(-1)
Which is: 30.199999999999999
[ FAILED ] MathTest.belowZeroCaboveZeroF (1 ms)
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用 EXPECT_NEAR,但不明白为什么上面的方法不起作用......
我有一个C++类,它以下列方式生成唯一的ID.
class Foo
{
static int seed;
public:
const int Uid;
Foo() : Uid(seed++) {}
}
int Foo::seed = 0;
Run Code Online (Sandbox Code Playgroud)
现在我使用Google Test测试此Id生成器:
Foo foo;
EXPECT_EQ(0, foo.Uid);
Foo foo2;
EXPECT_EQ(1, foo2.Uid);
Run Code Online (Sandbox Code Playgroud)
这个测试在我调试时通过但在我实际运行时失败,而是给我ID为2和3.有人可以帮我找出原因吗?Google测试是否背靠背运行其中两项测试?
我正在使用这里的以下示例
考虑我有以下课程
#include <iostream>
class Distance {
private:
int feet;
int inches;
public:
Distance() : feet(), inches() {}
Distance(int f, int i) : feet(f), inches(i) {}
friend std::ostream &operator<<( std::ostream &output, const Distance &D )
{
output << "F : " << D.feet << " I : " << D.inches;
return output;
}
friend std::istream &operator>>( std::istream &input, Distance &D )
{
input >> D.feet >> D.inches;
return input;
}
};
Run Code Online (Sandbox Code Playgroud)
我正在使用 Gtest 来测试这个类。
但我找不到更好的方法来测试它。
我可以使用 gtest 中提供的宏ASSERT_NO_THROW …
大家好,我有这个构造函数
Matrix::Matrix(size_t row, size_t col)
{
if(row < 1 || col < 1)
throw new std::runtime_error("Minimalni velikost matice je 1x1");
matrix = std::vector<std::vector< double > >(row,std::vector<double>(col, 0));
}
Run Code Online (Sandbox Code Playgroud)
和这个测试
Matrix *TestedMatrix;
EXPECT_THROW(TestedMatrix = new Matrix(-2,3),std::runtime_error );
Run Code Online (Sandbox Code Playgroud)
但我仍然得到那个例外是不同类型的。我也试过,std::runtime_error*
但结果是一样的。一开始我想使用 EXPECT_ANY_THROW 但它没有显示在我的代码覆盖率中。感谢帮助 !:)
上下文: 我正在尝试使用 GMock 模拟 OpenCV-C++ 类。
问题 :
我无法将 EXPECT_CALL 方法用于接受 cv::Mat 并返回 cv::Mat 的函数。编译器说 gmock-matcher不能从 cv::MatExpr 转换为 bool 作为 return。
以下是编译时的详细错误信息。
In file included from /home/arun/Documents /LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock-spec-builders.h:75:0,
from /home/arun/Documents/LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:43,
from /home/arun/Documents/LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock.h:61,
from /home/arun/Documents/LaneDetection/test/test.cpp:32:
/home/arun/Documents/LaneDetection/test/../vendor /googletest/googlemock/include/gmock/gmock-matchers.h: In instantiation of ‘bool
testing::internal::AnyEq::operator()(const A&, const B&) const [with A = cv::Mat; B = cv::Mat]’:
/home/arun/Documents/LaneDetection/test/../vendor/googletest/googlemock/include/gmock/gmock-matchers.h:908:18:
required from ‘bool testing::internal::ComparisonBase<D, Rhs, Op>::Impl<Lhs>::MatchAndExplain(Lhs, testing::MatchResultListener*) const [with Lhs = cv::Mat; D = testing::internal::EqMatcher<cv::Mat>; Rhs = cv::Mat; Op = testing::internal::AnyEq]’
/home/arun/Documents/LaneDetection/test/test.cpp:77:39: required from here …
Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以main()
在 C++ 中的 Visual Studio 2019 中进行单元测试?
我已经尝试#including
了 main.h 并main()
从测试中调用,但它“看起来”像是在main()
调用测试,从而导致递归。
我想很早就向学生介绍测试(编写代码使测试变得绿色),学生将(还)没有函数或类的经验。
仅供参考,我们正在使用 GoogleTest,但可以更改该选择。
c++ unit-testing program-entry-point googletest visual-studio
我只需要执行我创建的测试之一。但是,我没有找到如何运行它,因为我只找到了“Run_all_tests”。
我使用GoogleTest,我想用#ifndef里面测试一些函数.
档案交流
bool myFunction() {
#ifndef FOO
return true;
#else
return false;
#endif
}
Run Code Online (Sandbox Code Playgroud)
是否可以在特定测试期间强制#undef?就像我可以在2个统计数据中测试函数(使用define和不使用).
googletest ×8
c++ ×6
unit-testing ×2
c ×1
c++11 ×1
embedded ×1
gmock ×1
matrix ×1
opencv ×1
throw ×1