-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pw_unit_test: Support GTest backend in RPC server
Previously, the pw_unit_test RPC server didn't support GoogleTest at all, only the default ':light' backend. This adds initial support for GoogleTest. The core functionality (running tests) works, but there's still some functionality missing. In particular, we cannot run a subset of the test suites (filtering), because GoogleTest does not programmatically support this functionality. Additionally, there are a few differences in outputs -- GoogleTest does not support reporting all expectations (only failures), and the failure output of GoogleTest does not necessarily conform to the constrained "expected expression / actual expression" format that pw_unit_test requires. Change-Id: I8403c83fbdf90d3cb43bb67e0b6fbf0ba89e035d Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/100660 Reviewed-by: Wyatt Hepler <[email protected]> Commit-Queue: Eli Lipsitz <[email protected]>
- Loading branch information
Showing
9 changed files
with
217 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2022 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
#include <tuple> | ||
|
||
#include "gtest/gtest.h" | ||
#include "pw_unit_test/internal/rpc_event_handler.h" | ||
#include "pw_unit_test/unit_test_service.h" | ||
|
||
namespace pw::unit_test::internal { | ||
|
||
RpcEventHandler::RpcEventHandler(UnitTestService& service) : service_(service) { | ||
// Initialize GoogleTest and disable the default result printer. | ||
testing::InitGoogleTest(); | ||
auto unit_test = testing::UnitTest::GetInstance(); | ||
auto default_listener = unit_test->listeners().default_result_printer(); | ||
unit_test->listeners().Release(default_listener); | ||
delete default_listener; | ||
} | ||
|
||
void RpcEventHandler::ExecuteTests(span<std::string_view> suites_to_run) { | ||
if (!suites_to_run.empty()) { | ||
PW_LOG_WARN( | ||
"GoogleTest backend does not support test suite filtering. Running all " | ||
"suites."); | ||
} | ||
if (service_.verbose_) { | ||
PW_LOG_WARN( | ||
"GoogleTest backend does not support reporting passed expectations."); | ||
} | ||
|
||
auto unit_test = testing::UnitTest::GetInstance(); | ||
unit_test->listeners().Append(this); | ||
|
||
std::ignore = RUN_ALL_TESTS(); | ||
|
||
unit_test->listeners().Release(this); | ||
} | ||
|
||
void RpcEventHandler::OnTestProgramStart(const testing::UnitTest&) { | ||
service_.WriteTestRunStart(); | ||
} | ||
|
||
void RpcEventHandler::OnTestProgramEnd(const testing::UnitTest& unit_test) { | ||
RunTestsSummary run_tests_summary{ | ||
.passed_tests = unit_test.successful_test_count(), | ||
.failed_tests = unit_test.failed_test_count(), | ||
.skipped_tests = unit_test.skipped_test_count(), | ||
.disabled_tests = unit_test.disabled_test_count(), | ||
}; | ||
service_.WriteTestRunEnd(run_tests_summary); | ||
} | ||
|
||
void RpcEventHandler::OnTestStart(const testing::TestInfo& test_info) { | ||
TestCase test_case{ | ||
.suite_name = test_info.test_suite_name(), | ||
.test_name = test_info.name(), | ||
.file_name = test_info.file(), | ||
}; | ||
service_.WriteTestCaseStart(test_case); | ||
} | ||
|
||
void RpcEventHandler::OnTestEnd(const testing::TestInfo& test_info) { | ||
TestResult result; | ||
if (test_info.result()->Passed()) { | ||
result = TestResult::kSuccess; | ||
} else if (test_info.result()->Skipped()) { | ||
result = TestResult::kSkipped; | ||
} else { | ||
result = TestResult::kFailure; | ||
} | ||
|
||
service_.WriteTestCaseEnd(result); | ||
} | ||
|
||
void RpcEventHandler::OnTestPartResult(const testing::TestPartResult& result) { | ||
TestExpectation expectation{ | ||
.expression = "", | ||
.evaluated_expression = result.summary(), | ||
.line_number = result.line_number(), | ||
.success = result.passed(), | ||
}; | ||
service_.WriteTestCaseExpectation(expectation); | ||
} | ||
|
||
void RpcEventHandler::OnTestDisabled(const testing::TestInfo& test_info) { | ||
TestCase test_case{ | ||
.suite_name = test_info.test_suite_name(), | ||
.test_name = test_info.name(), | ||
.file_name = test_info.file(), | ||
}; | ||
service_.WriteTestCaseDisabled(test_case); | ||
} | ||
|
||
} // namespace pw::unit_test::internal |
44 changes: 44 additions & 0 deletions
44
pw_unit_test/rpc_gtest_public/pw_unit_test/internal/rpc_event_handler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2022 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
#pragma once | ||
|
||
#include "gtest/gtest.h" | ||
#include "pw_span/span.h" | ||
|
||
namespace pw::unit_test { | ||
|
||
class UnitTestService; | ||
|
||
namespace internal { | ||
|
||
// GoogleTest event handler that streams test events through an RPC service. | ||
class RpcEventHandler : public testing::EmptyTestEventListener { | ||
public: | ||
RpcEventHandler(UnitTestService& service); | ||
void ExecuteTests(span<std::string_view> suites_to_run); | ||
|
||
void OnTestProgramStart(const testing::UnitTest& unit_test) override; | ||
void OnTestProgramEnd(const testing::UnitTest& unit_test) override; | ||
void OnTestStart(const testing::TestInfo& test_info) override; | ||
void OnTestEnd(const testing::TestInfo& test_info) override; | ||
void OnTestPartResult( | ||
const testing::TestPartResult& test_part_result) override; | ||
void OnTestDisabled(const testing::TestInfo& test_info) override; | ||
|
||
private: | ||
UnitTestService& service_; | ||
}; | ||
|
||
} // namespace internal | ||
} // namespace pw::unit_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters