How to add timeout to test when using Google Testing framework
Asked Answered
O

3

13

I'm in need to test C++ code - and I've decided to use Google's testing framework. I need to make sure that a test doesn't hang due to a new bug. In the .NET testing framework it's possible to add [Timeout] on to of the test in order to make sure that it does not run for too long.

How can I create a simialr behavior when using Google Test?

Outrelief answered 5/6, 2013 at 14:8 Comment(1)
Possible duplicate of Time out for test cases in googletestPeipus
V
11

Google test does not offer something similar. http://code.google.com/p/googletest/issues/detail?id=348

You would have to add this functionality in C++

Violante answered 10/6, 2013 at 9:43 Comment(1)
re "you would have to [implement] this functionality in C++" here is an example implementation: #25852889 (the two questions should probably be merged somehow)Minard
A
3

You can try to set test properties through cmake: https://cmake.org/cmake/help/v3.8/prop_test/TIMEOUT.html

example:

set_tests_properties(${Tests} PROPERTIES TIMEOUT 10)

Aconite answered 4/10, 2019 at 15:14 Comment(1)
Problem with this approach is that within CI, if you rely on getting report files (json or xml) - and using ctest's timeout: no result file is written ..Vostok
C
-1

Building on what @ymochurad said, we ended up using the ctest cli optional argument:

ctest --timeout 1

to set the a default timeout for all tests. This was sufficient for our needs - hopefully it will help others.

For more info see: https://cmake.org/cmake/help/latest/manual/ctest.1.html

Craiova answered 11/2, 2023 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.