rust_gtest_interop_unittest_main.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/bind.h"
  5. #include "base/command_line.h"
  6. #include "base/strings/stringprintf.h"
  7. #include "base/test/launcher/unit_test_launcher.h"
  8. #include "base/test/test_suite.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include <iostream>
  11. // Update this when adding a new test to rust_test_interop_unittest.rs.
  12. int kNumTests = 11;
  13. bool is_subprocess() {
  14. // The test launching process spawns a subprocess to run tests, and it
  15. // includes this flag.
  16. return base::CommandLine::ForCurrentProcess()->HasSwitch(
  17. base::kGTestFlagfileFlag);
  18. }
  19. int main(int argc, char** argv) {
  20. // Run tests in a single process so we can count the tests.
  21. std::string single_process = base::StringPrintf("--test-launcher-jobs=1");
  22. // We verify that the test suite and test name written in the #[gtest] macro
  23. // is being propagated to Gtest by using a test filter that matches on the
  24. // test suites/names.
  25. std::string filter =
  26. base::StringPrintf("--gtest_filter=Test.*:ExactSuite.ExactTest");
  27. int my_argc = argc + 2;
  28. char** my_argv = new char*[argc];
  29. for (int i = 0; i < argc; ++i)
  30. my_argv = argv;
  31. my_argv[argc] = single_process.data();
  32. my_argv[argc + 1] = filter.data();
  33. base::TestSuite test_suite(my_argc, my_argv);
  34. int result = base::LaunchUnitTests(
  35. my_argc, my_argv,
  36. base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite)));
  37. if (is_subprocess()) {
  38. // Double-check that we actually ran all the tests. If this fails we'll see
  39. // all the tests marked as "fail on exit" since the whole process is
  40. // considered a failure.
  41. auto succeed = testing::UnitTest::GetInstance()->successful_test_count();
  42. int expected_success = kNumTests;
  43. if (succeed != expected_success) {
  44. std::cerr << "***ERROR***: Expected " << expected_success
  45. << " tests to succeed, but we saw: " << succeed << '\n';
  46. return 1;
  47. }
  48. }
  49. return result;
  50. }