Skip to content

How to set test timeout dynamically ? #443

@redboltz

Description

@redboltz

I am using Boost.Test version 1.87.0.
I am considering setting a uniform timeout for test cases. The purpose is to terminate a test case if it gets stuck due to some issue and proceed to the next test case.

However, I want to disable this timeout when debugging with a debugger such as gdb, as timeouts during debugging can interfere with the process.

According to the documentation:
https://www.boost.org/doc/libs/1_87_0/libs/test/doc/html/boost_test/testing_tools/timeout.html
it appears that timeouts can be specified using the described method.

First, I specified a timeout using a constant, following the example in the documentation.

This resulted in the expected timeout.
However, to disable the timeout for debugging, recompilation is required.

Next, I attempted to specify a variable instead of a compile-time constant.

This also resulted in the expected timeout.

To dynamically switch the timeout on or off and adjust its duration, I considered passing it as a command-line argument.

inline unsigned long get_timeout() {
    std::cout << "In get_timeout(), argc:" << boost::unit_test::framework::master_test_suite().argc << std::endl;
    if (boost::unit_test::framework::master_test_suite().argc > 1) {
        return
            std::stoul(
                boost::unit_test::framework::master_test_suite().argv[1]
            );
    }
    return 0;
}

However, in this case, the timeout did not take effect.

Upon analysis, it appears that:

BOOST_AUTO_TEST_CASE(t1, * boost::unit_test::timeout(get_timeout())) {

calls get_timeout(), but at this point,
boost::unit_test::framework::master_test_suite().argc and
boost::unit_test::framework::master_test_suite().argv
have not yet been initialized.

To confirm this, I added the following code inside the test case:

auto tout = get_timeout();
std::cout 
    << "In test case argc: "
    << boost::unit_test::framework::master_test_suite().argc 
    << " result of get_timeout(): "
    << tout
    << std::endl;

At this timing, argc and argv were correctly retrieved.

Is there a good way to dynamically switch the timeout?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions