Re: [googletest] What to write in headers of test code

1,818 views
Skip to first unread message

Keith Ray

unread,
May 25, 2013, 2:25:34 PM5/25/13
to Crab Man, googletes...@googlegroups.com
You don't need to write headers for your unit tests, unless your have classes or other code you want to shared. you don't need to #include unit tests into your main.cpp file. You might want to search stackoverflow for answers on how to build and link on whatever platform you are using.


On May 25, 2013, at 10:53 AM, Crab Man <headcr...@gmail.com> wrote:

Hello. I've been using gtest in my project. Right now All my tests are in different cpp files which are included by the main cpp file. I think everybody understands that it's not really a good thing. I want to know how can I write headers for those files which will be included in the main file so that I can compile these series of tests and link them later as I do with my usual code.

--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Crab Man

unread,
May 25, 2013, 2:34:17 PM5/25/13
to googletes...@googlegroups.com, Crab Man
Does it mean that I can just write tests, compile them, link them with each other and with 
int main(int argc, char** argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
(I don't use that dedicated gtest file that launches all tests, I have it written myself in main function) and get them all launched?
суббота, 25 мая 2013 г., 22:25:34 UTC+4 пользователь keith.ray написал:
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframework+unsub...@googlegroups.com.

Daniel Walker

unread,
May 25, 2013, 2:39:44 PM5/25/13
to Crab Man, Google C++ Testing Framework

Yes. If the other cpp files with tests in are built at the same time as the main.cpp file then they will run too.

The set up in terms of make file and what gets built (probably all the cpp files in the directory) works in just the same way as a normal non-test project.

To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.

Crab Man

unread,
May 25, 2013, 2:59:47 PM5/25/13
to googletes...@googlegroups.com
I followed your advices, removed includes and added secondary test .cpp files to add_executable command in CMakeLists.txt and it works. Thanks for your help.
However I still don't understand how it works and how does the main file find needed tests when he doesn't really know anything about them (in contrast to usual c++ linking while having header includes). And also I don't understand why if I add them to add_executable command they all are found but if I do add_library for each of them and then target_link_libraries them against executable, they don't run.

Keith Ray

unread,
May 25, 2013, 6:19:12 PM5/25/13
to Crab Man, googletes...@googlegroups.com
(Going by memory.) How it works: C++ initializes/constructs global and static variables before main() executes. Macros used to declare tests also create static variables that, as part of initialization, register themselves with a singleton known by the Google test code. At run-time, the test framework goes through all the registered objects that the singleton knows about, and executes them. Those objects create test objects, calls their SetUp and TearDown methods, etc.

I advise reading and tracing through the source code to understand how it works.

Also read:

http://agilesolutionspace.blogspot.com/2010/04/test-driving-c.html

http://agilesolutionspace.blogspot.com/2008/12/william-wake-described-essence-of.html

http://agilesolutionspace.blogspot.com/2011/04/coupling-or-what-you-dont-want-your.html

http://agilesolutionspace.blogspot.com/2011/04/objects-dependancies-are-their-own-tax.html

http://www.parashift.com/c++-faq/

http://www.amazon.com/gp/product/0321113586/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0321113586&linkCode=as2&tag=exploagilesol-20
On 2013 May 25, at 11:59 AM, Crab Man <headcr...@gmail.com> wrote:

> I followed your advices, removed includes and added secondary test .cpp files to add_executable command in CMakeLists.txt and it works. Thanks for your help.
> However I still don't understand how it works and how does the main file find needed tests when he doesn't really know anything about them (in contrast to usual c++ linking while having header includes). And also I don't understand why if I add them to add_executable command they all are found but if I do add_library for each of them and then target_link_libraries them against executable, they don't run.
>
Reply all
Reply to author
Forward
0 new messages