This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).
Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn
The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/
pal/Test/Scripts/unity_to_junit.py@2:b894b3508057, 2017-09-05 (annotated)
- Committer:
- Ren Boting
- Date:
- Tue Sep 05 11:56:13 2017 +0900
- Revision:
- 2:b894b3508057
- Parent:
- 0:29983394c6b6
Update all libraries and reform main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edamame22 | 0:29983394c6b6 | 1 | # ----------------------------------------------------------------------- |
edamame22 | 0:29983394c6b6 | 2 | # Copyright (c) 2016 ARM Limited. All rights reserved. |
edamame22 | 0:29983394c6b6 | 3 | # SPDX-License-Identifier: Apache-2.0 |
edamame22 | 0:29983394c6b6 | 4 | # Licensed under the Apache License, Version 2.0 (the License); you may |
edamame22 | 0:29983394c6b6 | 5 | # not use this file except in compliance with the License. |
edamame22 | 0:29983394c6b6 | 6 | # You may obtain a copy of the License at |
edamame22 | 0:29983394c6b6 | 7 | # |
edamame22 | 0:29983394c6b6 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
edamame22 | 0:29983394c6b6 | 9 | # |
edamame22 | 0:29983394c6b6 | 10 | # Unless required by applicable law or agreed to in writing, software |
edamame22 | 0:29983394c6b6 | 11 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
edamame22 | 0:29983394c6b6 | 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
edamame22 | 0:29983394c6b6 | 13 | # See the License for the specific language governing permissions and |
edamame22 | 0:29983394c6b6 | 14 | # limitations under the License. |
edamame22 | 0:29983394c6b6 | 15 | # ----------------------------------------------------------------------- |
edamame22 | 0:29983394c6b6 | 16 | |
edamame22 | 0:29983394c6b6 | 17 | import re |
edamame22 | 0:29983394c6b6 | 18 | import sys |
edamame22 | 0:29983394c6b6 | 19 | from tabulate import tabulate |
edamame22 | 0:29983394c6b6 | 20 | |
edamame22 | 0:29983394c6b6 | 21 | def extract_tags(logLines): |
edamame22 | 0:29983394c6b6 | 22 | |
edamame22 | 0:29983394c6b6 | 23 | # These are the Unity tag names we are going to look for. |
edamame22 | 0:29983394c6b6 | 24 | tagNames = [ |
edamame22 | 0:29983394c6b6 | 25 | "UnityTest", |
edamame22 | 0:29983394c6b6 | 26 | "UnityIgnoredTest", |
edamame22 | 0:29983394c6b6 | 27 | "UnityResult", |
edamame22 | 0:29983394c6b6 | 28 | ] |
edamame22 | 0:29983394c6b6 | 29 | |
edamame22 | 0:29983394c6b6 | 30 | # All tags will end up here. |
edamame22 | 0:29983394c6b6 | 31 | tags = [] |
edamame22 | 0:29983394c6b6 | 32 | |
edamame22 | 0:29983394c6b6 | 33 | for tagName in tagNames: |
edamame22 | 0:29983394c6b6 | 34 | |
edamame22 | 0:29983394c6b6 | 35 | # Fetch start and end locations for the start and end markers. |
edamame22 | 0:29983394c6b6 | 36 | |
edamame22 | 0:29983394c6b6 | 37 | startMatches = re.finditer(re.escape("<***" + tagName + "***>"), logLines) |
edamame22 | 0:29983394c6b6 | 38 | endMatches = re.finditer(re.escape("</***" + tagName + "***>"), logLines) |
edamame22 | 0:29983394c6b6 | 39 | |
edamame22 | 0:29983394c6b6 | 40 | startOffsets = [match.end() for match in startMatches] |
edamame22 | 0:29983394c6b6 | 41 | endOffsets = [match.start() - 1 for match in endMatches] |
edamame22 | 0:29983394c6b6 | 42 | |
edamame22 | 0:29983394c6b6 | 43 | # If the amount of start and end markers isn't identical, this is an error. |
edamame22 | 0:29983394c6b6 | 44 | if len(startOffsets) != len(endOffsets): |
edamame22 | 0:29983394c6b6 | 45 | raise Exception("For tag '" + tagName + "', start and end tags do not match!") |
edamame22 | 0:29983394c6b6 | 46 | |
edamame22 | 0:29983394c6b6 | 47 | # Append all found tags to the tags list. |
edamame22 | 0:29983394c6b6 | 48 | for tagOffsets in zip(startOffsets, endOffsets): |
edamame22 | 0:29983394c6b6 | 49 | tagContent = logLines[tagOffsets[0]: tagOffsets[1] + 1] |
edamame22 | 0:29983394c6b6 | 50 | tags.append((tagOffsets[0], tagName, tagContent)) |
edamame22 | 0:29983394c6b6 | 51 | |
edamame22 | 0:29983394c6b6 | 52 | # Sort the tags list by the offset. |
edamame22 | 0:29983394c6b6 | 53 | tags.sort(key=lambda tag_: tag_[0]) |
edamame22 | 0:29983394c6b6 | 54 | |
edamame22 | 0:29983394c6b6 | 55 | # Throw away the offset (once sorted, it is no longer needed). |
edamame22 | 0:29983394c6b6 | 56 | tags = [tag[1:] for tag in tags] |
edamame22 | 0:29983394c6b6 | 57 | |
edamame22 | 0:29983394c6b6 | 58 | # At this point we are left with list of tags, each one a pair, consisting of |
edamame22 | 0:29983394c6b6 | 59 | # (group name, test name, status). |
edamame22 | 0:29983394c6b6 | 60 | return tags |
edamame22 | 0:29983394c6b6 | 61 | |
edamame22 | 0:29983394c6b6 | 62 | def get_test_group_and_name(printableName): |
edamame22 | 0:29983394c6b6 | 63 | match = re.match(r"TEST\((.*), (.*)\)", printableName) |
edamame22 | 0:29983394c6b6 | 64 | if match is not None: |
edamame22 | 0:29983394c6b6 | 65 | return match.group(1), match.group(2) |
edamame22 | 0:29983394c6b6 | 66 | else: |
edamame22 | 0:29983394c6b6 | 67 | raise Exception("Erorr parsing test group and name") |
edamame22 | 0:29983394c6b6 | 68 | |
edamame22 | 0:29983394c6b6 | 69 | def get_ignored_test_group_and_name(printableName): |
edamame22 | 0:29983394c6b6 | 70 | match = re.match(r"IGNORE_TEST\((.*), (.*)\)", printableName) |
edamame22 | 0:29983394c6b6 | 71 | if match is not None: |
edamame22 | 0:29983394c6b6 | 72 | return match.group(1), match.group(2) |
edamame22 | 0:29983394c6b6 | 73 | else: |
edamame22 | 0:29983394c6b6 | 74 | raise Exception("Erorr parsing test group and name") |
edamame22 | 0:29983394c6b6 | 75 | |
edamame22 | 0:29983394c6b6 | 76 | def collect_tests(tags): |
edamame22 | 0:29983394c6b6 | 77 | |
edamame22 | 0:29983394c6b6 | 78 | tests = [] |
edamame22 | 0:29983394c6b6 | 79 | |
edamame22 | 0:29983394c6b6 | 80 | # Build the list of tests, with status for each. |
edamame22 | 0:29983394c6b6 | 81 | curTest = "" |
edamame22 | 0:29983394c6b6 | 82 | resultHandled = False |
edamame22 | 0:29983394c6b6 | 83 | for tag in tags: |
edamame22 | 0:29983394c6b6 | 84 | |
edamame22 | 0:29983394c6b6 | 85 | tagName = tag[0] |
edamame22 | 0:29983394c6b6 | 86 | tagValue = tag[1] |
edamame22 | 0:29983394c6b6 | 87 | |
edamame22 | 0:29983394c6b6 | 88 | if tagName == "UnityTest": |
edamame22 | 0:29983394c6b6 | 89 | curTest = get_test_group_and_name(tagValue) |
edamame22 | 0:29983394c6b6 | 90 | resultHandled = False |
edamame22 | 0:29983394c6b6 | 91 | elif tagName == "UnityResult": |
edamame22 | 0:29983394c6b6 | 92 | if not resultHandled: |
edamame22 | 0:29983394c6b6 | 93 | tests.append((curTest, tagValue)) |
edamame22 | 0:29983394c6b6 | 94 | resultHandled = True |
edamame22 | 0:29983394c6b6 | 95 | elif tagName == "UnityIgnoredTest": |
edamame22 | 0:29983394c6b6 | 96 | curTest = get_ignored_test_group_and_name(tagValue) |
edamame22 | 0:29983394c6b6 | 97 | tests.append((curTest, "IGNORE")) |
edamame22 | 0:29983394c6b6 | 98 | else: |
edamame22 | 0:29983394c6b6 | 99 | raise Exception("Unknown tag '" + tagName + "' encountered") |
edamame22 | 0:29983394c6b6 | 100 | |
edamame22 | 0:29983394c6b6 | 101 | return tests |
edamame22 | 0:29983394c6b6 | 102 | |
edamame22 | 0:29983394c6b6 | 103 | def generate_junit_xml_output(packageName, tests, xmlOutFile): |
edamame22 | 0:29983394c6b6 | 104 | |
edamame22 | 0:29983394c6b6 | 105 | testsCount = len(tests) |
edamame22 | 0:29983394c6b6 | 106 | |
edamame22 | 0:29983394c6b6 | 107 | with open(xmlOutFile, "wt") as f: |
edamame22 | 0:29983394c6b6 | 108 | print >> f, '<testsuite tests="' + str(testsCount) + '">' |
edamame22 | 0:29983394c6b6 | 109 | for test in tests: |
edamame22 | 0:29983394c6b6 | 110 | print >> f, ' <testcase classname="' + packageName + "." + test[0][0] + '" name="' + test[0][1] + '">' |
edamame22 | 0:29983394c6b6 | 111 | if test[1] == "FAIL": |
edamame22 | 0:29983394c6b6 | 112 | print >> f, ' <failure/>' |
edamame22 | 0:29983394c6b6 | 113 | if test[1] == "IGNORE": |
edamame22 | 0:29983394c6b6 | 114 | print >> f, ' <skipped/>' |
edamame22 | 0:29983394c6b6 | 115 | print >> f, ' </testcase>' |
edamame22 | 0:29983394c6b6 | 116 | print >> f, '</testsuite>' |
edamame22 | 0:29983394c6b6 | 117 | |
edamame22 | 0:29983394c6b6 | 118 | def generate_text_output(packageName, tests, textOutFile): |
edamame22 | 0:29983394c6b6 | 119 | |
edamame22 | 0:29983394c6b6 | 120 | testsCount = len(tests) |
edamame22 | 0:29983394c6b6 | 121 | |
edamame22 | 0:29983394c6b6 | 122 | failingTests = 0 |
edamame22 | 0:29983394c6b6 | 123 | passedTests = 0 |
edamame22 | 0:29983394c6b6 | 124 | ignoredTests = 0 |
edamame22 | 0:29983394c6b6 | 125 | |
edamame22 | 0:29983394c6b6 | 126 | testsTable = [] |
edamame22 | 0:29983394c6b6 | 127 | for test in tests: |
edamame22 | 0:29983394c6b6 | 128 | if test[1] == "FAIL": |
edamame22 | 0:29983394c6b6 | 129 | failingTests += 1 |
edamame22 | 0:29983394c6b6 | 130 | if test[1] == "PASS": |
edamame22 | 0:29983394c6b6 | 131 | passedTests += 1 |
edamame22 | 0:29983394c6b6 | 132 | if test[1] == "IGNORE": |
edamame22 | 0:29983394c6b6 | 133 | ignoredTests += 1 |
edamame22 | 0:29983394c6b6 | 134 | |
edamame22 | 0:29983394c6b6 | 135 | testsTable.append([test[0][0], test[0][1], test[1]]) |
edamame22 | 0:29983394c6b6 | 136 | |
edamame22 | 0:29983394c6b6 | 137 | resultsTableHeader = ["TOTAL", "PASS", "FAIL", "IGNORE"] |
edamame22 | 0:29983394c6b6 | 138 | resultsTable = [[str(testsCount), str(passedTests), str(failingTests), str(ignoredTests)]] |
edamame22 | 0:29983394c6b6 | 139 | |
edamame22 | 0:29983394c6b6 | 140 | with open(textOutFile, "wt") as f: |
edamame22 | 0:29983394c6b6 | 141 | |
edamame22 | 0:29983394c6b6 | 142 | print >> f, "==== " + packageName + " ====" |
edamame22 | 0:29983394c6b6 | 143 | print >> f, tabulate(testsTable) |
edamame22 | 0:29983394c6b6 | 144 | print >> f, tabulate(resultsTable, headers=resultsTableHeader) |
edamame22 | 0:29983394c6b6 | 145 | |
edamame22 | 0:29983394c6b6 | 146 | if testsCount == 0 or failingTests > 0: |
edamame22 | 0:29983394c6b6 | 147 | finalStatus = "FAIL" |
edamame22 | 0:29983394c6b6 | 148 | else: |
edamame22 | 0:29983394c6b6 | 149 | finalStatus = "PASS" |
edamame22 | 0:29983394c6b6 | 150 | print >> f |
edamame22 | 0:29983394c6b6 | 151 | print >> f, "Final status: " + finalStatus + "." |
edamame22 | 0:29983394c6b6 | 152 | |
edamame22 | 0:29983394c6b6 | 153 | def unity_to_junit(packageName, inputFile, xmlOutFile, textOutFile): |
edamame22 | 0:29983394c6b6 | 154 | |
edamame22 | 0:29983394c6b6 | 155 | with open(inputFile, "rt") as f: |
edamame22 | 0:29983394c6b6 | 156 | logLines = f.read() |
edamame22 | 0:29983394c6b6 | 157 | |
edamame22 | 0:29983394c6b6 | 158 | tags = extract_tags(logLines) |
edamame22 | 0:29983394c6b6 | 159 | tests = collect_tests(tags) |
edamame22 | 0:29983394c6b6 | 160 | |
edamame22 | 0:29983394c6b6 | 161 | generate_junit_xml_output(packageName, tests, xmlOutFile) |
edamame22 | 0:29983394c6b6 | 162 | generate_text_output(packageName, tests, textOutFile) |
edamame22 | 0:29983394c6b6 | 163 | |
edamame22 | 0:29983394c6b6 | 164 | |
edamame22 | 0:29983394c6b6 | 165 | if __name__ == "__main__": |
edamame22 | 0:29983394c6b6 | 166 | |
edamame22 | 0:29983394c6b6 | 167 | if len(sys.argv) != 5: |
edamame22 | 0:29983394c6b6 | 168 | print "Usage: <package-name> <input-file> <xml-out-file> <text-out-file>" |
edamame22 | 0:29983394c6b6 | 169 | sys.exit(1) |
edamame22 | 0:29983394c6b6 | 170 | |
edamame22 | 0:29983394c6b6 | 171 | unity_to_junit(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) |
edamame22 | 0:29983394c6b6 | 172 |