webcoverage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. include_once(dirname(__FILE__, 2) . "/test/lib/coverage.php");
  3. global $zentaoRoot;
  4. $zentaoRoot = dirname(__FILE__, 2);
  5. $type = isset($_GET['module']) ? 'module' : 'summary';
  6. $coverage = new coverage();
  7. $report = '';
  8. $ztfReport = $coverage->getZtfReport('web');
  9. if($ztfReport)
  10. {
  11. $ztfHtml = "<div class='report'><strong>%s</strong> 执行 <strong>%s个</strong>用例,耗时 <strong>%s秒</strong>。<strong>%s (%s%%) </strong>通过,<strong>%s (%s%%)</strong> 失败,<strong>%s (%s%%)</strong> 忽略。</div>";
  12. $ztfHtml = sprintf($ztfHtml, $ztfReport->time, $ztfReport->total, $ztfReport->duration, $ztfReport->pass, $ztfReport->passPercent, $ztfReport->fail, $ztfReport->failPercent, $ztfReport->skip, $ztfReport->skipPercent);
  13. }
  14. else
  15. {
  16. $ztfHtml = "<p>没有找到ZTF测试报告。</p>";
  17. }
  18. switch($type)
  19. {
  20. case 'summary':
  21. $report = $coverage->genWebSummaryReport();
  22. break;
  23. case 'module':
  24. $module = $_GET['module'];
  25. $file = $_GET['file'];
  26. $report = $coverage->genWebSummaryReport($module, $file);
  27. break;
  28. default:
  29. $report = $coverage->genWebSummaryReport();
  30. break;
  31. }
  32. ?>
  33. <!DOCTYPE html>
  34. <html lang="zh-cn" xml:lang="zh-cn">
  35. <head>
  36. <meta charset="UTF-8">
  37. <title>单元测试行覆盖率报告</title>
  38. </head>
  39. <style>
  40. body {font-family: Arial, sans-serif; font-size: 16px; line-height: 1.5; margin: 0; padding: 20px; }
  41. table {border-collapse: collapse; max-width: 100%; width: 100%; margin: 20px 0; }
  42. th {border: 1px solid #ccc; padding: 8px; text-align: center; background-color: #eee; white-space: nowrap; }
  43. caption{font-weight: bold; margin: 10px 0; font-size: 18px; }
  44. h2 {margin-top: 20px; font-size: 24px; }
  45. .red {color: red; }
  46. .green {color: green; }
  47. tbody tr:hover {background-color: #f5f5f5; }
  48. tbody tr:nth-child(even) {background-color: #f9f9f9; }
  49. h1 {text-align: center; }
  50. </style>
  51. <body>
  52. <h1>单元测试行覆盖率报告</h1>
  53. <?php
  54. echo $ztfHtml;
  55. echo $report;
  56. echo "<script>var type = '$type';</script>";
  57. ?>
  58. <script src="./js/jquery/lib.js"></script>
  59. <script>
  60. $().ready(function()
  61. {
  62. if(type == 'summary')
  63. {
  64. renderColorByCoveragePercent()
  65. implementExpand();
  66. implementSort();
  67. }
  68. });
  69. function implementSort()
  70. {
  71. var table = $('#summaryTable');
  72. var tbody = table.find('tbody');
  73. var rowsArr = tbody.find('tr').toArray();
  74. rowsArr.sort(function(row1, row2)
  75. {
  76. /* Get seventh row and translate it's value into int. */
  77. var val1 = $(row1).find('th:eq(0)').text();
  78. var val2 = $(row2).find('th:eq(0)').text();
  79. if (val1 < val2)
  80. {
  81. return -1;
  82. }
  83. else if (val1 > val2)
  84. {
  85. return 1;
  86. }
  87. else
  88. {
  89. return 0;
  90. }
  91. });
  92. $.each(rowsArr, function(index, row)
  93. {
  94. tbody.append(row);
  95. });
  96. }
  97. function renderColorByCoveragePercent()
  98. {
  99. $('table tbody tr td').each(function()
  100. {
  101. var text = $(this).text();
  102. if(text.indexOf('%') > -1)
  103. {
  104. var percent = parseInt(text);
  105. if(percent < 50)
  106. {
  107. $(this).css('color', 'red');
  108. }
  109. else if(percent < 80)
  110. {
  111. $(this).css('color', 'orange');
  112. }
  113. else
  114. {
  115. $(this).css('color', 'green');
  116. }
  117. }
  118. });
  119. }
  120. function implementExpand()
  121. {
  122. $("tr[name$='-child']").hide();
  123. $("tr[name$='-parent']").click(function()
  124. {
  125. $(this).nextUntil("tr[name$='-parent']").slideToggle('fast');
  126. });
  127. }
  128. </script>