adding test_printmsg

This commit is contained in:
poikjhn 2016-06-16 23:46:24 +02:00
parent 26a7bb3761
commit b14108fa93

View File

@ -41,26 +41,32 @@ class TestHelpers(unittest.TestCase):
self.assertFalse(is_int(''))
self.assertFalse(is_int('one'))
# @unittest.skip('skipping')
def test_sigint_handler(self):
# class for mocking stderr object
class StderrCapture:
capture = ""
def write(self, data):
self.capture += data
# assigning stderr to temp, mock object to stderr
sys.stderr, temp = StderrCapture(), sys.stderr
try:
# sending SIGINT to self
os.kill(os.getpid(), signal.SIGINT)
except SystemExit as err:
# assering exited with 1
self.assertEqual(err.args[0], 1)
# assering proper error message
self.assertEqual(sys.stderr.capture, "\nInterrupted.\n")
finally:
# reassigning stderr
sys.stderr = temp
def test_sigint_handler(capsys):
try:
# sending SIGINT to self
os.kill(os.getpid(), signal.SIGINT)
except SystemExit as error:
out, err = capsys.readouterr()
# assering exited with 1
assert error.args[0] == 1
# assering proper error message
assert out == ''
assert err == "\nInterrupted.\n"
def test_printmsg(capsys):
# call with two args
printmsg("test", "ERROR")
out, err = capsys.readouterr()
assert out == "\x1b[1mERROR: \x1b[21mtest\x1b[0m\n"
assert err == ''
# call with one arg
printmsg("message")
out, err = capsys.readouterr()
assert out == "message\n"
assert err == ''
if __name__ == "__main__":
unittest.main()