Skip to content

Commit 06e53bd

Browse files
committed
[test/dumper] Fix stdout testing issue
1 parent 0d69b54 commit 06e53bd

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

test/test_dumper.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import unittest
22
import sys
3-
import odml
4-
53
try:
64
from StringIO import StringIO
75
except ImportError:
86
from io import StringIO
97

8+
import odml
9+
10+
from odml.tools.dumper import dump_doc
11+
1012

1113
class TestTypes(unittest.TestCase):
1214

1315
def setUp(self):
14-
# Capture the output printed by the functions to STDOUT, and use it for
15-
# testing purposes.
16-
self.captured_stdout = StringIO()
17-
sys.stdout = self.captured_stdout
18-
1916
s_type = "type"
2017

2118
self.doc = odml.Document(author='Rave', version='1.0')
@@ -38,10 +35,19 @@ def setUp(self):
3835
self.doc.append(s1)
3936

4037
def test_dump_doc(self):
38+
# Capture the output printed by the functions to STDOUT, and use it for
39+
# testing purposes. It needs to be reset after the capture.
40+
captured_stdout = StringIO()
41+
sys.stdout = captured_stdout
42+
4143
# This test dumps the whole document and checks it word by word.
42-
# If possible, maybe some better way of testing this ?
43-
odml.tools.dumper.dump_doc(self.doc)
44-
output = [x.strip() for x in self.captured_stdout.getvalue().split('\n') if x]
44+
# If possible, maybe some better way of testing this?
45+
dump_doc(self.doc)
46+
output = [x.strip() for x in captured_stdout.getvalue().split('\n') if x]
47+
48+
# Reset stdout
49+
sys.stdout = sys.__stdout__
50+
4551
expected_output = []
4652
expected_output.append("*Cell (type='type')")
4753
expected_output.append(":Type (values=Rechargeable, dtype='string')")
@@ -50,10 +56,7 @@ def test_dump_doc(self):
5056
expected_output.append("*Electrode (type='type')")
5157
expected_output.append(":Material (values=Nickel, dtype='string')")
5258
expected_output.append(":Models (values=[AA,AAA], dtype='string')")
59+
5360
self.assertEqual(len(output), len(expected_output))
5461
for i in range(len(output)):
5562
self.assertEqual(output[i], expected_output[i])
56-
57-
# Discard the document output from stdout stream
58-
self.captured_stdout.seek(0)
59-
self.captured_stdout.truncate(0)

0 commit comments

Comments
 (0)