Skip to content

Commit

Permalink
fix custom attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
weiwei committed May 10, 2018
1 parent bf3e8df commit 7c2a32d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions junitparser/__init__.py
@@ -1,4 +1,4 @@
from .junitparser import (JUnitXmlError, Attr, Element, JUnitXml, TestSuite,
Property, Skipped, Failure, Error, TestCase, Properties)
Property, Skipped, Failure, Error, TestCase, Properties, IntAttr, FloatAttr)

__version__ = "1.2.0"
__version__ = "1.2.1"
8 changes: 6 additions & 2 deletions junitparser/junitparser.py
Expand Up @@ -72,7 +72,9 @@ class IntAttr(Attr):
"Integer attributes"
def __get__(self, instance, cls):
result = super(IntAttr, self).__get__(instance, cls)
if result is None:
if result is None and \
(isinstance(instance, JUnitXml) or
isinstance(instance, TestSuite)):
instance.update_statistics()
result = super(IntAttr, self).__get__(instance, cls)
return int(result) if result else None
Expand All @@ -86,7 +88,9 @@ class FloatAttr(Attr):
"Float attributes."
def __get__(self, instance, cls):
result = super(FloatAttr, self).__get__(instance, cls)
if result is None:
if result is None and \
(isinstance(instance, JUnitXml) or
isinstance(instance, TestSuite)):
instance.update_statistics()
result = super(FloatAttr, self).__get__(instance, cls)
return float(result) if result else None
Expand Down
18 changes: 17 additions & 1 deletion test.py
Expand Up @@ -8,7 +8,8 @@
import unittest
from copy import deepcopy
from junitparser import (TestCase, TestSuite, Skipped, Failure, Error, Attr,
JUnitXmlError, JUnitXml, Property, Properties)
JUnitXmlError, JUnitXml, Property, Properties, IntAttr,
FloatAttr)
from xml.etree import ElementTree as etree
from io import open
try:
Expand Down Expand Up @@ -627,5 +628,20 @@ def test_properties_ne2(self):
self.assertNotEqual(props1, props2)


class Test_Attrs(unittest.TestCase):

def test_attr(self):
TestCase.text = Attr("text")
TestCase.int = IntAttr("int")
TestCase.float = FloatAttr("float")
element = TestCase("foo")
element.text = "foo"
element.int = 10
element.float = 8.5
self.assertEqual(element.text, "foo")
self.assertEqual(element.int, 10)
self.assertEqual(element.float, 8.5)


if __name__ == '__main__':
unittest.main()

0 comments on commit 7c2a32d

Please sign in to comment.