google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
33.7k stars 9.98k forks source link

[FE]: Traced data in Value-Parameterized Tests #4436

Closed doctormo closed 6 months ago

doctormo commented 6 months ago

Does the feature exist in the most recent commit?

As far as I can search on github, and search engines; this feature does not exist and has not been proposed before.

Why do we need this feature?

Value-Parameterized Tests are a great way of developing testing machinery which can simplify and codify the writing of tests for common structures and interfaces. The problem is that when these tests fail, the information provided to the developer does not include the correct line number, and in the case of using a utility library, the right file.

Describe the proposal.

The proposal is to use a macro and a base struct to provide the optional capability to test writers to furnish their tests with the extra information.

The code samples here are provided as proof of concepts only and I declare them public domain and CC0 on the basis they are for education and demonstration.

Code that would go into gtest

struct traced_data
{
    const char *_file;
    const int _line;

    ::testing::ScopedTrace enable_scope() const
    {
        return ::testing::ScopedTrace(_file, _line, "");
    }
};

#define _P(type, ...) type{__FILE__, __LINE__, __VA_ARGS__}

Example of a utility library using it

struct example : ::testing::traced_data
{
    std::string cmp;
};

class examplePTest : public testing::TestWithParam<example> {};

 TEST_P(examplePTest, cmpString)
{
    auto test = GetParam();
    auto scope = test.enable_scope();
    EXPECT_EQ(test.cmp, "-");
}

Example final test suite

#include "util-library.h"

INSTANTIATE_TEST_SUITE_P(MyLocalTestSuite, examplePTest, testing::Values(
    _P(example, "+"),
    _P(example, "-"),
    _P(example, "=")
));

Example output

[----------] 3 tests from MyLocalTestSuite/examplePTest
[ RUN      ] MyLocalTestSuite/examplePTest.cmpString/0
/path/downstream-test.cpp:46: Failure
Expected equality of these values:
  test.cmp
    Which is: "+"
  "-"
Google Test trace:
/path/downstream-test.cpp:46: 
[  FAILED  ] MyLocalTestSuite/examplePTest.cmpString/0, where GetParam() = TEST-+ (0 ms)
[ RUN      ] MyLocalTestSuite/examplePTest.cmpString/1
[       OK ] MyLocalTestSuite/examplePTest.cmpString/1 (0 ms)
[ RUN      ] MyLocalTestSuite/examplePTest.cmpString/2
/path/downstream-test.cpp:48: Failure
Expected equality of these values:
  test.cmp
    Which is: "="
  "-"
Google Test trace:
/path/downstream-test.cpp:48:
[  FAILED  ] MyLocalTestSuite/examplePTest.cmpString/2, where GetParam() = TEST-= (0 ms)

Is the feature specific to an operating system, compiler, or build system version?

Not as far as I understand it.

derekmauro commented 6 months ago

This already exists. Support for stack traces on failure is available if you build with Abseil support. Some day we may do this using C++23's stack traces as well.

doctormo commented 6 months ago

Thanks. I'd love to see documentation about that.