gtest_xml_unittest_result_printer.h 2.2 KB

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