From 0b25a667e38cb6d78ba7b89a151b1ed811ad8832 Mon Sep 17 00:00:00 2001 From: EclectickEclipse Date: Thu, 23 Mar 2017 02:16:46 -0600 Subject: [PATCH 1/2] Added initial tests for mstats object. --- run_tests.py | 44 ++++++++++++ tests/test_mstats2.py | 162 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 run_tests.py create mode 100644 tests/test_mstats2.py diff --git a/run_tests.py b/run_tests.py new file mode 100644 index 0000000..4f1c44c --- /dev/null +++ b/run_tests.py @@ -0,0 +1,44 @@ +import argparse +import io +import os +import platform +import smtplib +import subprocess +import sys +import time +import unittest + +def output_system_info(log): + pipe = subprocess.PIPE + p = subprocess.Popen(['git', 'log', '--oneline', '-n 1'], stdout=pipe, + stderr=pipe) + stdout, stderr = p.communicate() + p.kill() + + print(item for item in [ + '%s\n' % time.asctime(), + 'os.name: %s\n' % os.name, + 'platform.system: %s\n' % platform.system(), + 'platform.release: %s\n' % platform.release(), + 'commit: %s\n' % stdout.decode().split()[0], + 'Python version (via sys.version): %s\n\n' % sys.version + ]) + + +def run_tests(log, v=2): + loader = unittest.TestLoader() + tests = loader.loadTestsFromName('builder') + runner = unittest.TextTestRunner(stream=log, buffer=True, verbosity=v) + runner.run(tests) + + +log = io.StringIO() +output_system_info(log) +run_tests(log) + +# UNCOMMENT TO ADD TO tests.log +# with open('logs/tests.log', 'a+') as test_log: +# l.test_logger.info('Appending test results to tests log.') +# test_log.writelines([line for line in log.getvalue()]) + +print(log.getvalue()) diff --git a/tests/test_mstats2.py b/tests/test_mstats2.py new file mode 100644 index 0000000..5b7752b --- /dev/null +++ b/tests/test_mstats2.py @@ -0,0 +1,162 @@ +import unittest +from unittest import TestCase + +from ping import MStats2 + + +class Mstats2Test(TestCase): + def setUp(self): + self.ms = MStats2() + + def test_init(self): + self.assertIsInstance(self.ms._this_ip, str) + self.assertEqual(self.ms._this_ip, '0.0.0.0') + + self.assertIsInstance(self.ms._timing_list, list) + self.assertEqual(self.ms._timing_list, []) + + self.assertIsInstance(self.ms._packets_sent, int) + self.assertEqual(self.ms._packets_sent, 0) + + self.assertIsInstance(self.ms._packets_rcvd, int) + self.assertEqual(self.ms._packets_rcvd, 0) + + # Statistics + self.assertIsInstance(self.ms._total_time, type(None)) + self.assertIsInstance(self.ms._mean_time, type(None)) + self.assertIsInstance(self.ms._median_time, type(None)) + self.assertIsInstance(self.ms._pstdev_time, type(None)) + self.assertIsInstance(self.ms._frac_loss, type(None)) + + def test_get_thisIP(self): + self.assertEqual(self.ms.thisIP, self.ms._this_ip) + + def test_set_thisIP(self): + self.ms.thisIP = '127.0.0.1' + self.assertEqual(self.ms.thisIP, '127.0.0.1') + + def test_get_pktsSent(self): + self.assertEqual(self.ms.pktsSent, self.ms._packets_sent) + + def test_get_pktsRcvd(self): + self.assertEqual(self.ms.pktsRcvd, self.ms._packets_rcvd) + + def test_get_pktsLost(self): + self.assertEqual(self.ms.pktsLost, + self.ms._packets_sent - self.ms._packets_rcvd) + + def test_get_minTime(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + self.assertEqual(self.ms.minTime, 1) + + def test_get_maxTime(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + self.assertEqual(self.ms.maxTime, 3) + + def test_get_totTime(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + self.assertEqual(self.ms.totTime, 6) + + def test__get_mean_time(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + mean_time = 6 / 3 + + self.assertEqual(self.ms._get_mean_time(), mean_time) + self.assertEqual(self.ms.mean_time, self.ms._get_mean_time()) + self.assertEqual(self.ms.avrgTime, self.ms._get_mean_time()) + + def test_get_median_time(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + median = sorted(self.ms._timing_list)[len(self.ms._timing_list)//2] + + self.assertEqual(self.ms._calc_median_time(), median) + + self.assertEqual(self.ms.median_time, median) + + def test_get_pstdev_time(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + # tests _calc_sum_square_time + sum_square = self.ms._calc_sum_square_time() + self.assertEqual(sum_square, 2) + + # tests _calc_pstdev_time + pstdev = self.ms._calc_pstdev_time() + self.assertEqual(pstdev, 0.816496580927726) + + # ensures pstdev_time + self.assertEqual(self.ms.pstdev_time, pstdev) + + def test_get_fracLoss(self): + self.ms._packets_sent += 1 + + loss = self.ms.pktsLost / self.ms.pktsSent + + self.assertEqual(self.ms.fracLoss, loss) + + def test_packet_sent(self): + self.ms.packet_sent(2) + self.assertEqual(self.ms.pktsSent, 2) + + def test_packet_received(self): + self.ms.packet_received(2) + self.assertEqual(self.ms.pktsRcvd, 2) + + def test_record_time(self): + self.ms.record_time(1) + self.assertEqual(len(self.ms._timing_list), 1) + + def test__reset_statistics(self): + self.ms._reset_statistics() + + self.assertEqual(self.ms._total_time, None) + self.assertEqual(self.ms._mean_time, None) + self.assertEqual(self.ms._median_time, None) + self.assertEqual(self.ms._pstdev_time, None) + self.assertEqual(self.ms._frac_loss, None) + + def test_calc_median_time(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + median = sorted(self.ms._timing_list)[len(self.ms._timing_list) // 2] + + self.assertEqual(self.ms._calc_median_time(), median) + + def test__calc_sum_square_time(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + sum_square = self.ms._calc_sum_square_time() + self.assertEqual(sum_square, 2) + + def test__calc_pstdev_time(self): + self.ms._timing_list.append(1) + self.ms._timing_list.append(2) + self.ms._timing_list.append(3) + + pstdev = self.ms._calc_pstdev_time() + self.assertEqual(pstdev, 0.816496580927726) + + +if __name__ == '__main__': + unittest.main() From abd0a326c0d907a0877120edbf9d455e559cfd48 Mon Sep 17 00:00:00 2001 From: EclectickEclipse Date: Thu, 23 Mar 2017 02:22:49 -0600 Subject: [PATCH 2/2] Fixed tests loading in run_tests --- run_tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/run_tests.py b/run_tests.py index 4f1c44c..ca91c89 100644 --- a/run_tests.py +++ b/run_tests.py @@ -1,13 +1,12 @@ -import argparse import io import os import platform -import smtplib import subprocess import sys import time import unittest + def output_system_info(log): pipe = subprocess.PIPE p = subprocess.Popen(['git', 'log', '--oneline', '-n 1'], stdout=pipe, @@ -15,7 +14,7 @@ def output_system_info(log): stdout, stderr = p.communicate() p.kill() - print(item for item in [ + log.writelines(item for item in [ '%s\n' % time.asctime(), 'os.name: %s\n' % os.name, 'platform.system: %s\n' % platform.system(), @@ -27,7 +26,8 @@ def output_system_info(log): def run_tests(log, v=2): loader = unittest.TestLoader() - tests = loader.loadTestsFromName('builder') + tests = loader.discover('tests') + # tests = loader.loadTestsFromName('builder') runner = unittest.TextTestRunner(stream=log, buffer=True, verbosity=v) runner.run(tests)