So last night I thought I would have a crack and writing a simple script to list out the longest running Maven tests. I started writing this in BASH but thought that Python would be a better option. This is the first time I have written any Python so it was a fun experience. This was tested using Python 2.7.1 and surefire reports based on Maven 3.0.4.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import glob | |
import re | |
filePatternToTest="TEST-*" | |
timeRegex="time=\"[0-9]*(\.[0-9]*)*\"" | |
classNameRegex="classname=\"[A-Za-z0-9\.]*\"" | |
nameRegex="name=\"[A-Za-z0-9_]*\"" | |
testCases = [] | |
def main(): | |
files = findFiles() | |
readFiles(files) | |
testCases = sortTestCases() | |
testCases.reverse() | |
for item in testCases: | |
print item | |
def findFiles(): | |
return glob.glob(filePatternToTest) | |
def readFiles(files): | |
for file in files: | |
with open(file, "rb") as openedFile: | |
content = openedFile.read() | |
openedFile.closed | |
parseContent(content) | |
def parseContent(content): | |
matches = re.findall("<testcase .*/>", content) | |
for testCase in matches: | |
value = getValues(testCase) | |
testCases.append(value) | |
def getValues(testCase): | |
timeAttribute = re.search(timeRegex, testCase) | |
if timeAttribute != None: | |
timeAttribute = timeAttribute.group(0) | |
classnameAttribute = re.search(classNameRegex, testCase) | |
if classnameAttribute != None: | |
classnameAttribute = classnameAttribute.group(0) | |
nameAttribute = re.search(nameRegex, testCase) | |
if nameAttribute != None: | |
nameAttribute = nameAttribute.group(0) | |
return tuple([timeAttribute, classnameAttribute, nameAttribute]) | |
def sortTestCases(): | |
return sorted(testCases, key=lambda row: row[0]) | |
main() |
After running the test phase of your Maven project, simply copy the script below into the target/surefire-reports directory. It scans all the XML files and orders the tests from longest running to shortest.
Some side notes about Python, what a fast language to pick up and start prototyping with. Within a couple of hours I went from having no experience with Python to a nice little script.
This script has gone on my GitHub account so if you find any bugs or improvements, make a post and I will update it.
https://github.com/joevartuli/MavenTestTime
No comments:
Post a Comment