Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aaa2d37

Browse files
[result] fix subtest referencing objects (#220)
* [result] fix subtest referencing objects * [tests] reproducing #219
1 parent 7723be8 commit aaa2d37

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

‎tests/testsuite.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ def test_subTest_mixed(self):
178178
with self.subTest(i=i):
179179
self.assertLess(i, 1, msg='this is a subtest.')
180180

181+
def test_subTest_with_dots(self):
182+
for i in range(2):
183+
with self.subTest(module='hello.world.subTest{}'.format(i)):
184+
self.fail('this is a subtest.')
185+
181186
class DecoratedUnitTest(unittest.TestCase):
182187

183188
@some_decorator
@@ -553,6 +558,27 @@ def test_unittest_subTest_pass(self):
553558
suite.addTest(self.DummySubTest('test_subTest_pass'))
554559
self._test_xmlrunner(suite)
555560

561+
@unittest.skipIf(not hasattr(unittest.TestCase, 'subTest'),
562+
'unittest.TestCase.subTest not present.')
563+
def test_unittest_subTest_with_dots(self):
564+
# Test for issue #85
565+
suite = unittest.TestSuite()
566+
suite.addTest(self.DummySubTest('test_subTest_with_dots'))
567+
outdir = BytesIO()
568+
569+
self._test_xmlrunner(suite, outdir=outdir)
570+
571+
xmlcontent = outdir.getvalue().decode()
572+
573+
# Method name
574+
self.assertNotIn('name="subTest', xmlcontent, 'parsing of test method name is not done correctly')
575+
self.assertIn('name="test_subTest_with_dots (module=\'hello.world.subTest', xmlcontent)
576+
577+
# Class name
578+
matchString = 'classname="tests.testsuite.XMLTestRunnerTestCase.DummySubTest.test_subTest_with_dots (module=\'hello.world"'
579+
self.assertNotIn(matchString, xmlcontent, 'parsing of class name is not done correctly')
580+
self.assertIn('classname="tests.testsuite.XMLTestRunnerTestCase.DummySubTest"', xmlcontent)
581+
556582
def test_xmlrunner_pass(self):
557583
suite = unittest.TestSuite()
558584
suite.addTest(self.DummyTest('test_pass'))

‎xmlrunner/result.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import sys
66
import datetime
7-
import time
87
import traceback
98
import re
109
from os import path
@@ -517,7 +516,11 @@ def _test_method_name(test_id):
517516
"""
518517
Returns the test method name.
519518
"""
520-
return test_id.split('.')[-1]
519+
# Trick subtest referencing objects
520+
subtest_parts = test_id.split(' ')
521+
test_method_name = subtest_parts[0].split('.')[-1]
522+
subtest_method_name = [test_method_name] + subtest_parts[1:]
523+
return ' '.join(subtest_method_name)
521524

522525
_test_method_name = staticmethod(_test_method_name)
523526

@@ -543,7 +546,10 @@ def _report_testcase(test_result, xml_testsuite, xml_document):
543546
xml_testsuite.appendChild(testcase)
544547

545548
class_name = re.sub(r'^__main__.', '', test_result.id())
546-
class_name = class_name.rpartition('.')[0]
549+
550+
# Trick subtest referencing objects
551+
class_name = class_name.split(' ')[0].rpartition('.')[0]
552+
547553
testcase.setAttribute('classname', class_name)
548554
testcase.setAttribute(
549555
'name', _XMLTestResult._test_method_name(test_result.test_id)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /