Skip to content

Instantly share code, notes, and snippets.

@azakordonets
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save azakordonets/803fc2a39627eacf499f to your computer and use it in GitHub Desktop.

Select an option

Save azakordonets/803fc2a39627eacf499f to your computer and use it in GitHub Desktop.
I've created this small script to get a list of failed test suites from Team City. We are using there Allure reports and when i provide username, password and url to "All artifacts" of the job, i can get a sorted list of failed test suites in the console. Handy when you need to re-run only failed test suites
#!/usr/bin/env python
import urllib
import os.path
import sys
import json
import shutil
import getopt
import argparse
import re
from urlparse import parse_qs, urlparse
class TeamCityFailedTests:
DOWNLOAD_URL = ''
DOWNLOAD_PATH = os.path.expanduser('~/Downloads/temp')
ARCHIVE_NAME = 'artifacts.zip'
TEST_SUITE_FILES_PATH = "%s/data" % DOWNLOAD_PATH
FILES = []
FAILED_TEST_SUITES = []
def ensure_download_path_exists(self):
if not os.path.exists(self.DOWNLOAD_PATH):
os.mkdir(self.DOWNLOAD_PATH)
def download_artifacts(self):
testfile = urllib.URLopener()
self.ensure_download_path_exists()
testfile.retrieve(self.DOWNLOAD_URL, "%s/%s" % (self.DOWNLOAD_PATH, self.ARCHIVE_NAME))
def get_list_of_test_suites(self):
print "Moving to %s" % (self.DOWNLOAD_PATH)
os.chdir(self.DOWNLOAD_PATH)
print 'Extracting files ...'
print "unzip -u -qq %s" % (self.ARCHIVE_NAME)
os.system("unzip -u -qq %s" % (self.ARCHIVE_NAME))
print 'Moving to data folder'
os.chdir(self.TEST_SUITE_FILES_PATH)
print 'Removing all files except test suite json files'
os.system("find . -type f ! -name '*-testcase.json' -delete")
for file in os.listdir(self.TEST_SUITE_FILES_PATH):
self.FILES.append(file)
def parse_files(self):
print 'Listing all failed test suites'
for file in self.FILES:
with open(file) as data_file:
data = json.load(data_file)
if data['status'] == 'FAILED' or data['status'] == 'BROKEN':
attachments = data['attachments'][0]
log_file_name = attachments['title'].split("/")[-1:][0]
self.FAILED_TEST_SUITES.append(log_file_name.split("Test.test")[0] + "Test")
for failed_test_suite_name in sorted(set(self.FAILED_TEST_SUITES)):
print failed_test_suite_name
def get_List_of_failed_test_suites(self):
print "Downloading archive from : %s url " % self.DOWNLOAD_URL
self.download_artifacts()
self.ensure_download_path_exists()
if os.path.exists(os.path.join(self.DOWNLOAD_PATH, self.ARCHIVE_NAME)):
print 'Found downloaded archive...'
self.get_list_of_test_suites()
self.parse_files()
self.cleanup()
sys.exit()
def cleanup(self):
print 'Cleaning up ...'
shutil.rmtree(self.DOWNLOAD_PATH)
def init_variables(self):
parser = argparse.ArgumentParser()
parser.add_argument('-url', '--url')
args = vars(parser.parse_args())
job_url = args['url']
host = "%s://%s" %(urlparse(job_url)[0] , urlparse(job_url)[1])
params = parse_qs(urlparse(job_url).query, keep_blank_values=False)
buildTypeId = params['buildTypeId'][0]
buildId = params['buildId'][0]
self.DOWNLOAD_URL = '%s/guestAuth/repository/downloadAll/%s/%s:id/%s' % (host, buildTypeId, buildId, self.ARCHIVE_NAME)
if __name__ == '__main__':
teamCityFailedTests = TeamCityFailedTests()
teamCityFailedTests.init_variables()
teamCityFailedTests.get_List_of_failed_test_suites()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment