test_reporting_testsuite.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2020 Intel Corporation
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. '''
  6. This test file contains testcases for reporting functionality of Testsuite class of twister
  7. '''
  8. import sys
  9. import os
  10. import xml.etree.ElementTree as ET
  11. import csv
  12. from collections import defaultdict
  13. from mock import MagicMock
  14. import pytest
  15. ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
  16. sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/schemas/twister/"))
  17. def test_discard_report(class_testsuite, platforms_list, all_testcases_dict, caplog, tmpdir):
  18. """ Testing discard_report function of Testsuite class in twister
  19. Test 1: Check if apply_filters function has been run before running
  20. discard_report
  21. Test 2: Test if the generated report is not empty
  22. Test 3: Test if the gerenrated report contains the expected columns"""
  23. class_testsuite.platforms = platforms_list
  24. class_testsuite.testcases = all_testcases_dict
  25. filename = tmpdir.mkdir("test_discard").join("discard_report.csv")
  26. with pytest.raises(SystemExit):
  27. class_testsuite.discard_report(filename)
  28. assert "apply_filters() hasn't been run!" in caplog.text
  29. kwargs = {"exclude_tag" : ['test_a'], "exclude_platform" : ['demo_board_1'],
  30. "platform" : ['demo_board_2']}
  31. class_testsuite.apply_filters(**kwargs)
  32. class_testsuite.discard_report(filename)
  33. assert os.stat(filename).st_size != 0
  34. with open(filename, "r") as file:
  35. csv_reader = csv.reader(file)
  36. assert set(['test', 'arch', 'platform', 'reason']) == set(list(csv_reader)[0])
  37. def test_csv_report(class_testsuite, instances_fixture, tmpdir):
  38. """ Testing csv_report function of Testsuite class in twister
  39. Test 1: Assert the csv_report isnt empty after execution of csv_report function
  40. Test 2: Assert on the columns and values of the generated csv_report"""
  41. class_testsuite.instances = instances_fixture
  42. filename = tmpdir.mkdir("test_csv").join("twister_csv_report.csv")
  43. class_testsuite.csv_report(filename)
  44. assert os.path.exists(filename)
  45. assert os.stat(filename).st_size != 0
  46. mydict = {'test': [], 'arch' : [], 'platform' : [], 'status': [],
  47. 'extra_args': [], 'handler': [], 'handler_time': [],
  48. 'ram_size': [], 'rom_size': []}
  49. with open(filename, "r") as file:
  50. csv_reader = csv.reader(file)
  51. assert set(mydict.keys()) == set(list(csv_reader)[0])
  52. for instance in class_testsuite.instances.values():
  53. mydict["test"].append(instance.testcase.name)
  54. mydict["arch"].append(instance.platform.arch)
  55. mydict["platform"].append(instance.platform.name)
  56. instance_status = instance.status if instance.status is not None else ""
  57. mydict["status"].append(instance_status)
  58. args = " ".join(instance.testcase.extra_args)
  59. mydict["extra_args"].append(args)
  60. mydict["handler"].append(instance.platform.simulation)
  61. mydict["handler_time"].append(instance.metrics.get("handler_time", ""))
  62. mydict["ram_size"].append(instance.metrics.get("ram_size", '0'))
  63. mydict["rom_size"].append(instance.metrics.get("rom_size", '0'))
  64. dict_file = open(filename, "r")
  65. dict_reader = csv.DictReader(dict_file)
  66. columns = defaultdict(list)
  67. for row in dict_reader:
  68. for (key, value) in row.items():
  69. columns[key].append(value)
  70. for _, value in enumerate(mydict):
  71. assert columns[value] == mydict[value]
  72. dict_file.close()
  73. def test_xunit_report(class_testsuite, test_data,
  74. instances_fixture, platforms_list, all_testcases_dict):
  75. """ Testing xunit_report function of Testsuite class in twister
  76. Test 1: Assert twister.xml file exists after execution of xunit_report function
  77. Test 2: Assert on fails, passes, skips, errors values
  78. Test 3: Assert on the tree structure of twister.xml file"""
  79. class_testsuite.platforms = platforms_list
  80. class_testsuite.testcases = all_testcases_dict
  81. kwargs = {"exclude_tag" : ['test_a'], "exclude_platform" : ['demo_board_1'],
  82. "platform" : ['demo_board_2']}
  83. class_testsuite.apply_filters(**kwargs)
  84. class_testsuite.instances = instances_fixture
  85. inst1 = class_testsuite.instances.get("demo_board_2/scripts/tests/twister/test_data/testcases/tests/test_a/test_a.check_1")
  86. inst2 = class_testsuite.instances.get("demo_board_2/scripts/tests/twister/test_data/testcases/tests/test_a/test_a.check_2")
  87. inst1.status = "failed"
  88. inst2.status = "skipped"
  89. filename = test_data + "twister.xml"
  90. fails, passes, errors, skips = class_testsuite.xunit_report(filename)
  91. assert os.path.exists(filename)
  92. filesize = os.path.getsize(filename)
  93. assert filesize != 0
  94. tree = ET.parse(filename)
  95. assert int(tree.findall('testsuite')[0].attrib["skipped"]) == int(skips)
  96. assert int(tree.findall('testsuite')[0].attrib["failures"]) == int(fails)
  97. assert int(tree.findall('testsuite')[0].attrib["errors"]) == int(errors)
  98. assert int(tree.findall('testsuite')[0].attrib["tests"]) == int(passes+fails+skips+errors)
  99. for index in range(1, len(class_testsuite.instances)+1):
  100. # index=0 corresponds to 'properties'. Test cases start from index=1
  101. if len(list(tree.findall('testsuite')[0][index])) != 0:
  102. if tree.findall('testsuite')[0][index][0].attrib["type"] == "failure":
  103. assert tree.findall('testsuite')[0][index].attrib["name"] == \
  104. (inst1.testcase.name)
  105. elif tree.findall('testsuite')[0][index][0].attrib["type"] == "skipped":
  106. assert tree.findall('testsuite')[0][index].attrib["name"] == \
  107. (inst2.testcase.name)
  108. os.remove(filename)
  109. def test_compare_metrics(class_testsuite, test_data, instances_fixture, caplog):
  110. """ Testing compare_metrics function of Testsuite class in twister
  111. Test 1: Error message is raised if file twister.csv file doesnt exist
  112. Test 2: Assert on compare_metrics results for expected values"""
  113. class_testsuite.instances = instances_fixture
  114. for instance in class_testsuite.instances.values():
  115. instance.metrics["ram_size"] = 5
  116. instance.metrics["rom_size"] = 9
  117. filename_not_exist = test_data + "twister_file_not_exist.csv"
  118. class_testsuite.compare_metrics(filename_not_exist)
  119. assert "Cannot compare metrics, " + filename_not_exist + " not found" in caplog.text
  120. filename = test_data + "twister.csv"
  121. results = class_testsuite.compare_metrics(filename)
  122. for instance in class_testsuite.instances.values():
  123. for res in results:
  124. assert res[0].platform.name == instance.platform.name
  125. if (res[0].platform.name == instance.platform.name) and \
  126. (res[0].testcase.name == instance.testcase.name):
  127. if res[1] == "ram_size":
  128. assert res[2] == instance.metrics["ram_size"]
  129. elif res[1] == "rom_size":
  130. assert res[2] == instance.metrics["rom_size"]
  131. def test_target_report(class_testsuite, instances_fixture, tmpdir_factory):
  132. """ Testing target_report function of Testsuite class in twister
  133. Test: Assert xunit_report function is called from target_report function"""
  134. class_testsuite.instances = instances_fixture
  135. outdir = tmpdir_factory.mktemp("tmp")
  136. class_testsuite.xunit_report = MagicMock(side_effect=class_testsuite.xunit_report)
  137. class_testsuite.target_report(outdir, "abc", append=False)
  138. assert class_testsuite.instances
  139. class_testsuite.xunit_report.assert_called()