gtest_xml_unittest_result_printer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2013 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. #ifndef BASE_TEST_GTEST_XML_UNITTEST_RESULT_PRINTER_H_
  5. #define BASE_TEST_GTEST_XML_UNITTEST_RESULT_PRINTER_H_
  6. #include <stdio.h>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/threading/thread_checker.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace base {
  11. class FilePath;
  12. // Generates an XML output file. Format is very close to GTest, but has
  13. // extensions needed by the test launcher.
  14. class XmlUnitTestResultPrinter : public testing::EmptyTestEventListener {
  15. public:
  16. XmlUnitTestResultPrinter();
  17. XmlUnitTestResultPrinter(const XmlUnitTestResultPrinter&) = delete;
  18. XmlUnitTestResultPrinter& operator=(const XmlUnitTestResultPrinter&) = delete;
  19. ~XmlUnitTestResultPrinter() override;
  20. static XmlUnitTestResultPrinter* Get();
  21. // Add link in the gtest xml output.
  22. // Please see AddLinkToTestResult in gtest_links.h for detailed
  23. // explanation and usage.
  24. void AddLink(const std::string& name, const std::string& url);
  25. // Must be called before adding as a listener. Returns true on success.
  26. [[nodiscard]] bool Initialize(const FilePath& output_file_path);
  27. // CHECK/DCHECK failed. Print file/line and message to the xml.
  28. void OnAssert(const char* file,
  29. int line,
  30. const std::string& summary,
  31. const std::string& message);
  32. private:
  33. // testing::EmptyTestEventListener:
  34. void OnTestCaseStart(const testing::TestCase& test_case) override;
  35. void OnTestStart(const testing::TestInfo& test_info) override;
  36. void OnTestEnd(const testing::TestInfo& test_info) override;
  37. void OnTestCaseEnd(const testing::TestCase& test_case) override;
  38. void WriteTestPartResult(const char* file,
  39. int line,
  40. testing::TestPartResult::Type type,
  41. const std::string& summary,
  42. const std::string& message);
  43. static XmlUnitTestResultPrinter* instance_;
  44. raw_ptr<FILE> output_file_{nullptr};
  45. bool open_failed_{false};
  46. ThreadChecker thread_checker_;
  47. };
  48. } // namespace base
  49. #endif // BASE_TEST_GTEST_XML_UNITTEST_RESULT_PRINTER_H_