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
  • You could fix the include path by adding the folder containing 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
  • This may help you get to a solution: https://stackoverflow.com/questions/40759562/gtest-test-project-link-to-other-executable – drescherjm Commented Jan 8 at 17:23
Add a comment  | 

1 Answer 1

Reset to default 2

I would actually restructure your code a bit. Since your screenshot shows Visual Studio I'll use that terminology

  1. 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;
    }
    
  1. your_math_test - executable

    math_test.cpp

    #include "your_math.h"
    #include "gtest/gtest.h"
    
    TEST(Math, Equals) {
       EXPECT_EQ(Add(2, 3), 5);
    }
    
  2. 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.

  1. your_math - this is a static library that declares and defines your math functions
  2. your_math_test - this is an executable that contains a gtest main, and it will link against your your_math static lib
  3. 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