Skip to content

Commit 5d6ddac

Browse files
committed
Use logging in tests rather than print()
Fix #251, Close #252.
1 parent fac55e5 commit 5d6ddac

File tree

7 files changed

+35
-20
lines changed

7 files changed

+35
-20
lines changed

cybox/test/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
# See LICENSE.txt for complete terms.
33

44
import json
5+
import logging
56
import unittest
67

78
from mixbox.binding_utils import ExternalEncoding
89
from mixbox.vendor import six
910

1011
from cybox import Entity, EntityList, TypedField
11-
import cybox.bindings.cybox_core as core_binding
12-
from cybox.core import Observables
1312
import cybox.utils
1413

14+
logger = logging.getLogger(__name__)
15+
1516

1617
def assert_equal_ignore(item1, item2, ignore_keys=None):
1718
"""Recursively compare two dictionaries, ignoring differences in some keys.
@@ -54,8 +55,8 @@ def round_trip(o, output=False, list_=False):
5455

5556
klass = o.__class__
5657
if output:
57-
print("Class: ", klass)
58-
print("-" * 40)
58+
logger.debug("Class: {0}".format(klass))
59+
logger.debug("-" * 40)
5960

6061
# 1. cybox.Entity -> dict/list
6162
if list_:
@@ -67,8 +68,8 @@ def round_trip(o, output=False, list_=False):
6768
json_string = json.dumps(d)
6869

6970
if output:
70-
print(json_string)
71-
print("-" * 40)
71+
logger.debug(json_string)
72+
logger.debug("-" * 40)
7273

7374
# Before parsing the JSON, make sure the cache is clear
7475
cybox.utils.cache_clear()
@@ -94,8 +95,8 @@ def round_trip(o, output=False, list_=False):
9495
xml_string = xml_string.decode(ExternalEncoding)
9596

9697
if output:
97-
print(xml_string)
98-
print("-" * 40)
98+
logger.debug(xml_string)
99+
logger.debug("-" * 40)
99100

100101
# Before parsing the XML, make sure the cache is clear
101102
cybox.utils.cache_clear()

cybox/test/common/hash_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
22
# See LICENSE.txt for complete terms.
33

4+
import logging
45
import unittest
56

67
from mixbox.vendor.six import u
78

89
from cybox.common import Hash, HashList, HashName, HexBinary
910
import cybox.test
1011

12+
logger = logging.getLogger(__name__)
13+
1114
EMPTY_MD5 = u("d41d8cd98f00b204e9800998ecf8427e")
1215
EMPTY_SHA1 = u("da39a3ee5e6b4b0d3255bfef95601890afd80709")
1316
EMPTY_SHA224 = u("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
@@ -174,10 +177,10 @@ def test_namespace_count(self):
174177
h.append(EMPTY_SHA256)
175178
h.append(EMPTY_SHA384)
176179
h.append(EMPTY_SHA512)
177-
print(h.to_xml())
180+
logger.info(h.to_xml())
178181

179182
ns_list = cybox.test.round_trip(h, list_=True)._get_namespaces()
180-
print(ns_list)
183+
logger.info(ns_list)
181184

182185
# Only "common" and "vocabs" should be here. "xsi" is only added later
183186
self.assertEqual(2, len(ns_list))

cybox/test/common/structured_test_text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import unittest
55

6-
from mixbox.vendor.six import u
6+
from mixbox.vendor.six import text_type, u
77

88
from cybox.common import StructuredText
99
import cybox.test
@@ -38,7 +38,7 @@ def test_plain(self):
3838
def test_unicode(self):
3939
text = self.klass.from_dict(self._full_dict)
4040
# This should not raise any errors
41-
print(text)
41+
self.assertTrue(b"WARNING" in text_type(text).encode('utf-8'))
4242

4343

4444
if __name__ == "__main__":

cybox/test/core/action_reference_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
22
# See LICENSE.txt for complete terms.
33

4+
import logging
45
import unittest
56

67
from mixbox.vendor.six import u
78

89
from cybox.core import ActionReference
910
from cybox.test import EntityTestCase
1011

12+
logger = logging.getLogger(__name__)
13+
1114

1215
class TestActionReference(EntityTestCase, unittest.TestCase):
1316
klass = ActionReference
@@ -17,8 +20,8 @@ class TestActionReference(EntityTestCase, unittest.TestCase):
1720

1821
def test_construction(self):
1922
aref = ActionReference(action_id="example:Action-1")
20-
print(aref.to_xml())
21-
print(aref.to_dict())
23+
logger.info(aref.to_xml())
24+
logger.info(aref.to_dict())
2225
self.assertTrue(b"example:Action-1" in aref.to_xml())
2326
self.assertTrue("example:Action-1" in aref.to_json())
2427

cybox/test/core/object_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
22
# See LICENSE.txt for complete terms.
33

4+
import logging
45
import unittest
56

67
from mixbox.vendor.six import u
78

8-
from cybox.core import Object, Observables, RelatedObject, Relationship
9+
from cybox.core import Object, Observables, RelatedObject
910
from cybox.objects.address_object import Address
10-
from cybox.objects.email_message_object import EmailMessage
1111
from cybox.objects.uri_object import URI
1212
from cybox.test import EntityTestCase, round_trip, round_trip_dict
1313
from cybox.utils import CacheMiss, set_id_method
1414

15+
logger = logging.getLogger(__name__)
16+
1517

1618
class ObjectTest(EntityTestCase, unittest.TestCase):
1719
klass = Object
@@ -152,7 +154,7 @@ def test_relationship_vocabnameref(self):
152154

153155
def _test_round_trip(self, observables):
154156
self.maxDiff = None
155-
print(observables.to_xml())
157+
logger.info(observables.to_xml())
156158
observables2 = round_trip(observables)
157159
self.assertEqual(observables.to_dict(), observables2.to_dict())
158160

cybox/test/core/observable_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
22
# See LICENSE.txt for complete terms.
33

4+
import logging
45
import unittest
56

67
from mixbox.vendor.six import u
@@ -12,6 +13,8 @@
1213
from cybox.objects.address_object import Address
1314
from cybox.test import EntityTestCase, round_trip
1415

16+
logger = logging.getLogger(__name__)
17+
1518

1619
class TestObservable(EntityTestCase, unittest.TestCase):
1720
klass = Observable
@@ -44,7 +47,7 @@ def test_keywords(self):
4447

4548
self.assertTrue(b"eyword" not in o.to_xml())
4649
o.add_keyword("Foo")
47-
print(o.to_xml())
50+
logger.info(o.to_xml())
4851
self.assertTrue(b"<cybox:Keyword>Foo</cybox:Keyword>" in o.to_xml())
4952

5053
o2 = round_trip(o)

cybox/test/objects/email_message_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# See LICENSE.txt for complete terms.
33

44
import datetime
5+
import logging
56
import unittest
67

78
from mixbox.vendor.six import u
@@ -17,6 +18,8 @@
1718
from cybox.test import EntityTestCase
1819
from cybox.test.objects import ObjectTestCase
1920

21+
logger = logging.getLogger(__name__)
22+
2023

2124
class TestLinks(EntityTestCase, unittest.TestCase):
2225
klass = LinkReference
@@ -301,10 +304,10 @@ def test_get_namespaces(self):
301304
m.links.append(u.parent.id_)
302305

303306
o = Observables([u, m])
304-
print(o.to_xml())
307+
logger.info(o.to_xml())
305308
actual_namespaces = o._get_namespaces()
306309

307-
print("\n".join([str(x) for x in actual_namespaces]))
310+
logger.info("\n".join([str(x) for x in actual_namespaces]))
308311

309312
self.assertEqual(5, len(actual_namespaces))
310313

0 commit comments

Comments
 (0)