Skip to content
Snippets Groups Projects
Commit 1409d8a9 authored by Mattia Mancini's avatar Mattia Mancini
Browse files

Story SW-300: add classes to test the std output from python UnitTest

framework
parent 4b11832d
No related branches found
No related tags found
2 merge requests!89Monitoring maintenance Epic branch merge,!1Resolve OSB-13 "Monitoringmaintenance "
...@@ -1830,6 +1830,7 @@ LCU/Maintenance/MDB_tools/cli/probe_mdb.py -text ...@@ -1830,6 +1830,7 @@ LCU/Maintenance/MDB_tools/cli/probe_mdb.py -text
LCU/Maintenance/MDB_tools/deploy.sh -text LCU/Maintenance/MDB_tools/deploy.sh -text
LCU/Maintenance/MDB_tools/fabfile.py -text LCU/Maintenance/MDB_tools/fabfile.py -text
LCU/Maintenance/MDB_tools/requirements.txt -text LCU/Maintenance/MDB_tools/requirements.txt -text
LCU/Maintenance/MDB_tools/test/output_testing.py -text
LCU/Maintenance/MDB_tools/test/python-coverage.sh eol=lf LCU/Maintenance/MDB_tools/test/python-coverage.sh eol=lf
LCU/Maintenance/MDB_tools/test/t_mdb_loader.in.test_rtsm.data -text LCU/Maintenance/MDB_tools/test/t_mdb_loader.in.test_rtsm.data -text
LCU/Maintenance/MDB_tools/test/t_mdb_loader.in.test_stationtest.data -text LCU/Maintenance/MDB_tools/test/t_mdb_loader.in.test_stationtest.data -text
......
...@@ -7,3 +7,4 @@ file(COPY ...@@ -7,3 +7,4 @@ file(COPY
lofar_add_test(t_mdb_loader) lofar_add_test(t_mdb_loader)
lofar_add_test(t_probe_mdb) lofar_add_test(t_probe_mdb)
# coding=utf-8
import mock
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
class compare_output_with:
"""
This class purpose is to create a 'with' environment to assert that the output is equal to the one specified
in the class constructor
"""
def __init__(self, expected_stdout):
"""
Class constructor takes a path to a text file that contains the expected std output
:param expected_stdout: expected standard output
:type expected_stdout: str
"""
self.expected_stdout = expected_stdout
def __enter__(self):
self.stdout_patcher = mock.patch('sys.stdout', new=StringIO())
self.stdout_patcher.start()
def __exit__(self, exc_type, exc_val, exc_tb):
stdout_produced = self.stdout_patcher.target.stdout.getvalue()
self.stdout_patcher.stop()
assert self.expected_stdout == stdout_produced, \
"expected stdout {} differs from {}".format(repr(self.expected_stdout), repr(stdout_produced))
class compare_output_with_file_content(compare_output_with):
"""
This class purpose is to create a 'with' environment to assert that the output is equal to the one specified
in content of the file_path specified in the class constructor
"""
def __init__(self, file_path):
"""
Class constructor takes a path to a text file that contains the expected std output
:param file_path: path to the file containing the expected standard output
"""
try:
with open(file_path, 'r') as fstream_in:
content = fstream_in.read()
except IOError as e:
raise Exception('Cannot run test - IOError on file {}: {}'.format(file_path, e))
super(compare_output_with_file_content, self).__init__(content)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment