admin管理员组文章数量:1129454
My console application has 2 files:
Program.h
int Add(int x, int b);
Program.cpp
#include <iostream>
int Add(int x, int b)
{
return x * b;
}
int main()
{
int x = 4;
int y = 5;
std::cout << x << " add " << y << " is " << Add(x, y) << std::endl;
}
This program runs fine, but I want to test Add()
with GoogleTest.
Visual Studio comes with the GoogleTest project type, so I created one but I can't #include "Program.h" in this test project.
The code block is #include "Program.h"
.
What's the easiest way to test a console program in GoogleTest?
I believe I can create NTFS hard/soft links to the files in the ConsoleApplication folder, but the trick may not work with Git or if I moves the solution around.
My console application has 2 files:
Program.h
int Add(int x, int b);
Program.cpp
#include <iostream>
int Add(int x, int b)
{
return x * b;
}
int main()
{
int x = 4;
int y = 5;
std::cout << x << " add " << y << " is " << Add(x, y) << std::endl;
}
This program runs fine, but I want to test Add()
with GoogleTest.
Visual Studio comes with the GoogleTest project type, so I created one but I can't #include "Program.h" in this test project.
The code block is #include "Program.h"
.
What's the easiest way to test a console program in GoogleTest?
I believe I can create NTFS hard/soft links to the files in the ConsoleApplication folder, but the trick may not work with Git or if I moves the solution around.
Share Improve this question edited Jan 8 at 18:02 genpfault 52.1k12 gold badges91 silver badges147 bronze badges asked Jan 8 at 17:14 Jason ChoJason Cho 331 silver badge6 bronze badges New contributor Jason Cho is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2 |1 Answer
Reset to default 2I would actually restructure your code a bit. Since your screenshot shows Visual Studio I'll use that terminology
your_math - static library
your_math.h
int Add(int x, int b);
your_math.cpp
#include "your_math.h" int Add(int x, int b) { return x * b; }
your_math_test - executable
math_test.cpp
#include "your_math.h" #include "gtest/gtest.h" TEST(Math, Equals) { EXPECT_EQ(Add(2, 3), 5); }
main - executable
program.cpp
#include "your_math.h" int main() { int x = 4; int y = 5; std::cout << x << " add " << y << " is " << Add(x, y) << std::endl; }
This way you have three build units, each with its own vcxproj.
- your_math - this is a static library that declares and defines your math functions
- your_math_test - this is an executable that contains a gtest main, and it will link against your
your_math
static lib - main - this contains the main executable for your console program, it also links against the
your_math
static lib
本文标签: googletestHow to test a C console programStack Overflow
版权声明:本文标题:googletest - How to test a C++ console program? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736699758a1948366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Program.h
to the "Additional Include Directories" setting of the GoogleTest project however then I expect linker errors since you can't link to an executable in native code. – drescherjm Commented Jan 8 at 17:22