admin管理员组文章数量:1413614
I'm using Google Test with parameterized tests and want to see the correct line number in the test output so I can quickly find the specific test case that failed.
Problem
When a test fails, the output shows the line number of the closing bracket of INSTANTIATE_TEST_SUITE_P
rather than the line where the specific test case is declared.
#include <gtest/gtest.h>
#include <string>
class TracedName {
public:
TracedName(std::string name, std::string filename, int line):
m_name(std::move(name)), m_filename(std::move(filename)), m_lineNumber(line)
{
}
std::string_view operator()() const { return m_name; }
::testing::ScopedTrace trace() const
{
return {m_filename.c_str(), m_lineNumber, m_name.c_str()};
}
private:
std::string m_name;
std::string m_filename;
int m_lineNumber;
};
#define TRACED_NAME(name) TracedName(name, __FILE__, __LINE__)
struct TestInput {
std::string value;
};
struct TestCase {
TracedName name;
TestInput input;
bool expected;
};
class ParameterizedTestSuite : public ::testing::TestWithParam<TestCase> {};
TEST_P(ParameterizedTestSuite, Test) {
const auto& param = GetParam();
const auto trace = param.name.trace(); // Trying to get the trace to point to the test case
EXPECT_EQ(param.input.value == "valid", param.expected);
}
INSTANTIATE_TEST_SUITE_P(
MyTests,
ParameterizedTestSuite,
::testing::Values(
TestCase{
.name = TRACED_NAME("ValidInput"),
.input = {.value = "valid"},
.expected = true,
},
TestCase{
.name = TRACED_NAME("InvalidInput"), // I want error message to point to THIS line
.input = {.value = "invalid"},
.expected = true, // This will fail
}
)
);
Current Behavior
When the test fails, I get an output with line numbers pointing to where INSTANTIATE_TEST_SUITE_P
ends, not where the failing test case is declared.
Expected Behavior
I want the output to show the line number where TRACED_NAME("InvalidInput")
is defined, so I can quickly go to the failing test case when looking at the terminal output.
I suspect the problem is caused by nested macro calls. Is this even possible?
with GCC the problem is not reproduced. The actual project uses 14.42.34433 MSVC Build Tools, there the printed line is not correct
I'm using Google Test with parameterized tests and want to see the correct line number in the test output so I can quickly find the specific test case that failed.
Problem
When a test fails, the output shows the line number of the closing bracket of INSTANTIATE_TEST_SUITE_P
rather than the line where the specific test case is declared.
#include <gtest/gtest.h>
#include <string>
class TracedName {
public:
TracedName(std::string name, std::string filename, int line):
m_name(std::move(name)), m_filename(std::move(filename)), m_lineNumber(line)
{
}
std::string_view operator()() const { return m_name; }
::testing::ScopedTrace trace() const
{
return {m_filename.c_str(), m_lineNumber, m_name.c_str()};
}
private:
std::string m_name;
std::string m_filename;
int m_lineNumber;
};
#define TRACED_NAME(name) TracedName(name, __FILE__, __LINE__)
struct TestInput {
std::string value;
};
struct TestCase {
TracedName name;
TestInput input;
bool expected;
};
class ParameterizedTestSuite : public ::testing::TestWithParam<TestCase> {};
TEST_P(ParameterizedTestSuite, Test) {
const auto& param = GetParam();
const auto trace = param.name.trace(); // Trying to get the trace to point to the test case
EXPECT_EQ(param.input.value == "valid", param.expected);
}
INSTANTIATE_TEST_SUITE_P(
MyTests,
ParameterizedTestSuite,
::testing::Values(
TestCase{
.name = TRACED_NAME("ValidInput"),
.input = {.value = "valid"},
.expected = true,
},
TestCase{
.name = TRACED_NAME("InvalidInput"), // I want error message to point to THIS line
.input = {.value = "invalid"},
.expected = true, // This will fail
}
)
);
Current Behavior
When the test fails, I get an output with line numbers pointing to where INSTANTIATE_TEST_SUITE_P
ends, not where the failing test case is declared.
Expected Behavior
I want the output to show the line number where TRACED_NAME("InvalidInput")
is defined, so I can quickly go to the failing test case when looking at the terminal output.
I suspect the problem is caused by nested macro calls. Is this even possible?
https://godbolt./z/8Tfqesh7e with GCC the problem is not reproduced. The actual project uses 14.42.34433 MSVC Build Tools, there the printed line is not correct
Share Improve this question edited Mar 11 at 18:32 Sergey Kolesnik asked Mar 11 at 17:06 Sergey KolesnikSergey Kolesnik 3,7392 gold badges14 silver badges44 bronze badges 5 |1 Answer
Reset to default 2This is not an issue with Google Test, but rather how __LINE__
is interpreted by the compiler in a macro expansion (in your case INSTANTIATE_TEST_SUITE_P
). gcc and clang see __LINE__
at the expected location, while MSVC puts it on the closing parentheses of the macro.
std::source_location::line()
gives yet again other results, and there gcc and clang do not agree anymore.
Example (godbolt):
1. #include <iostream>
2. #include <source_location>
3.
4. using namespace std;
5.
6. #define PRINT_LINE cout << "__LINE__ macro = " << __LINE__ << endl;
7. #define PRINT_SRC_LOC cout << "source_location macro = " << 8. source_location::current().line() << endl;
8.
9. #define DO_STUFF(x) x
10.
11. int main() {
12. DO_STUFF(
13.
14. PRINT_LINE PRINT_SRC_LOC cout << "__LINE__ direct = " << __LINE__ << endl; cout << "source_location direct = " << source_location::current().line() << endl;
15.
16. )
17. }
gcc 14.2:
__LINE__ macro = 14
source_location macro = 12
__LINE__ direct = 14
source_location direct = 12
clang 20.1.0:
__LINE__ macro = 14
source_location macro = 16
__LINE__ direct = 14
source_location direct = 16
MSVC 19.40:
__LINE__ macro = 16
source_location macro = 12
__LINE__ direct = 16
source_location direct = 12
So: __LINE__
works as expected for gcc/clang, while MSVC sees it at the closing parenthesis. Moreover, std::source_location::line()
is seen by gcc/MSVC at the opening parenthesis, while clang sees it at the closing parenthesis.
To me the C++ standard sounds as if std::source_location::line()
should give the same result as __LINE__
, but it doesn't.
Moreover, the standard doesn't seem to define the interaction between macro expansion and __LINE__
, compare this answer. As an additional note, C (rather than C++) also does not really define the behavior, see e.g. this gcc discussion and the references therein (especially N2115).
The simplest solution would be to store the ::testing::Values(...)
in a global variable (with internal linkage, i.e. marked as static
or in an anonymous namespace, so that it cannot clash with identical names in other translation units). As a result, the TRACED_NAME
is not within an argument of the macro INSTANTIATE_TEST_SUITE_P
, and the line numbers will be correct:
static const auto MyTestValues = ::testing::Values(
TestCase{
.name = TRACED_NAME("ValidInput"),
.input = {.value = "valid"},
.expected = true,
},
TestCase{
.name = TRACED_NAME("InvalidInput"),
.input = {.value = "invalid"},
.expected = true,
}
);
INSTANTIATE_TEST_SUITE_P(
MyTests,
ParameterizedTestSuite,
MyTestValues
);
本文标签: cGoogle Test How to get correct line numbers in test output for parameterized testsStack Overflow
版权声明:本文标题:c++ - Google Test: How to get correct line numbers in test output for parameterized tests? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780529a2624682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
INSTANTIATE_TEST_SUITE_P
ends". – 3CxEZiVlQ Commented Mar 11 at 17:22<< param.name();
, I can't replicate godbolt./z/6xYh8hdnE -/app/example.cpp:54: InvalidInput
looks well. Please post a minimal reproducible example. – 3CxEZiVlQ Commented Mar 11 at 17:24friend void PrintTo(const TestCase& self, std::ostream* ostr){ *ostr << self.name; }
instead of macros - gtest will print you the name on fail (with the added bonus of printing the nice name when executed with--gtest_list_tests
): godbolt./z/K8zvvv81P. – Yksisarvinen Commented Mar 11 at 17:27