user3470960
user3470960

Reputation:

How do you run Google Test through the command line?

I have a sample project with Google Test up and running. I have a simple test class that looks like:

#include <gtest/gtest.h>

TEST(FirstTest, TestNumberOne){
  EXPECT_NE(2, 1);
}

This is in a directory on my computer at /home/dave/Desktop/sandbox/black-test/src/code/Tester.cpp

I'm working in Eclipse with Ubuntu. The test class runs fine out of Eclipse (right click, run as C++). But I can't find documentation on how to run via the command prompt?

Secondarily, do most people who do unit testing for C++ keep their test project separate from their production code or coupled (like most Java projects)?

Upvotes: 11

Views: 35972

Answers (1)

harmic
harmic

Reputation: 30597

Inside your test program, you will have a main() function which looks something like this:

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

This allows you to invoke the test program like any other: just type the name of the executable in a shell.

Depending on how you have things organised, you could have multiple c++ source files containing tests which are linked together, in which case there should be only one main function. Google test provides command line options to specify which tests to execute.

The wiki has lots of info on this.

I personally prefer to keep tests in a separate directory tree but it is really up to personal preference.

Upvotes: 9

Related Questions