dimanche 21 décembre 2014

Python HTMLTestRunner test report contains only partial test case names

I'm trying to setup a unit test with reports generated by HTMLTestRunner. The tests are running fine but the generated report doesn't contain full test case names, instead only the last part of the test case name (the part after the last dot) is printed in the report.


Test case names are generated dynamically in the form of: test_ + user agent string. User agent strings contain spaces and dots and this is the root cause for incomplete test case names in the report.


Any ideas how I could get the full test case names in the report without doing any string replacement for the user agent names? I need to be able to easily copy/paste original user agents from the report.


HTMLTestRunner report


HTMLTestRunner Report


Terminal output



ok test_HTC_Desire_X Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 (__main__.TestUserAgents)
F test_Mozilla/5.0 (Android; Mobile; rv:24.0) Gecko/24.0 Firefox/24.0 (__main__.TestUserAgents)
F test_Mozilla/5.0 (Linux; Android 4.1.2; GT-I9105P Build/JZO54K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19 (__main__.TestUserAgents)
ok test_Mozilla/5.0 (Linux; U; Android 4.0.4; nl-nl; GT-N8010 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30 (__main__.TestUserAgents)
F test_Opera/9.80 (Android; Opera Mini/7.5.33361/31.1448; U; en) Presto/2.8.119 Version/11.1010 (__main__.TestUserAgents)


Test cases are generated dynamically like that:



import unittest
import csv
import requests
import HTMLTestRunner


class TestUserAgents(unittest.TestCase):
'''Placeholder class. Test methods are added dynamically
via for/in loop at the end of the file.
'''
pass

def create_test(ua):
url = 'http://example.com'
def test(self):
user_agent = {'User-agent': '%s' % ua}
response = requests.get(url, headers = user_agent)
self.assertTrue('teststring' not in response.url)
return test

def suite():
s1 = unittest.TestLoader().loadTestsFromTestCase(TestUserAgents)
return unittest.TestSuite([s1])

def run(suite, report = "report.html"):
with open(report, "w") as f:
HTMLTestRunner.HTMLTestRunner(
stream = f,
title = 'Test User Agents Report',
verbosity = 2,
description = 'Test UserAgents description'
).run(suite)

if __name__ == '__main__':
ua_file = open('user_agents_sample.csv')
user_agents = csv.reader(ua_file)

for os, browser, ua in user_agents:
test_func = create_test(ua)
setattr(TestUserAgents, 'test_{0}'.format(ua), test_func)

run(suite())

Aucun commentaire:

Enregistrer un commentaire