-
Notifications
You must be signed in to change notification settings - Fork 14.4k
/
get_all_tests.py
78 lines (64 loc) · 2.41 KB
/
get_all_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
Gets all tests cases from xunit file.
"""
from __future__ import annotations
import sys
from xml.etree import ElementTree
def last_replace(s, old, new, number_of_occurrences):
"""
Replaces last n occurrences of the old string with the new one within the string provided
:param s: string to replace occurrences with
:param old: old string
:param new: new string
:param number_of_occurrences: how many occurrences should be replaced
:return: string with last n occurrences replaced
"""
list_of_components = s.rsplit(old, number_of_occurrences)
return new.join(list_of_components)
def print_all_cases(xunit_test_file_path):
"""
Prints all test cases read from the xunit test file
:param xunit_test_file_path: path of the xunit file
:return: None
"""
with open(xunit_test_file_path) as file:
text = file.read()
root = ElementTree.fromstring(text)
test_cases = root.findall(".//testcase")
classes = set()
modules = set()
for test_case in test_cases:
the_module = test_case["classname"].rpartition(".")[0]
the_class = last_replace(test_case.get("classname"), ".", ":", 1)
test_method = test_case.get("name")
modules.add(the_module)
classes.add(the_class)
print(the_class + "." + test_method)
for the_class in classes:
print(the_class)
for the_module in modules:
print(the_module)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Please provide name of xml unit file as first parameter")
sys.exit(1)
file_name = sys.argv[1]
print_all_cases(file_name)