From 55ee2ba76545af6aa5fcb64f7c56edd574e47cf5 Mon Sep 17 00:00:00 2001 From: Kyan Date: Mon, 9 Aug 2021 18:36:27 +0800 Subject: [PATCH] Update testcases --- tests/test_command_line.py | 24 ++++++++++++------------ tests/test_ping3.py | 3 +-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/test_command_line.py b/tests/test_command_line.py index 737b7eb..3c9c1c5 100644 --- a/tests/test_command_line.py +++ b/tests/test_command_line.py @@ -28,13 +28,13 @@ def test_dest_addr_0(self): def test_dest_addr_1(self): with patch("sys.stdout", new=io.StringIO()) as fake_out: command_line.main(["127.0.0.1"]) - self.assertTrue("127.0.0.1" in fake_out.getvalue()) + self.assertIn("127.0.0.1", fake_out.getvalue()) def test_dest_addr_2(self): with patch("sys.stdout", new=io.StringIO()) as fake_out: command_line.main(["127.0.0.1", "8.8.8.8"]) - self.assertTrue("127.0.0.1" in fake_out.getvalue()) - self.assertTrue("8.8.8.8" in fake_out.getvalue()) + self.assertIn("127.0.0.1", fake_out.getvalue()) + self.assertIn("8.8.8.8", fake_out.getvalue()) def test_count(self): with patch("sys.stdout", new=io.StringIO()) as fake_out: @@ -49,7 +49,7 @@ def test_timeout(self): def test_ttl(self): with patch("sys.stdout", new=io.StringIO()) as fake_out: command_line.main(['-T', '1', 'example.com']) - self.assertRegex(fake_out.getvalue(), r".*Timeout.*") + self.assertRegex(fake_out.getvalue(), r".*Error.*") def test_size(self): with patch("sys.stdout", new=io.StringIO()) as fake_out: @@ -61,10 +61,10 @@ def test_size(self): def test_interval(self): with patch("sys.stdout", new=io.StringIO()) as fake_out: start_time = time.time() - command_line.main(['-i', '1.7', 'example.com']) + command_line.main(['-i', '1', 'example.com']) end_time = time.time() - self.assertTrue((end_time - start_time) >= 5.1) # time_expect = (count - 1) * interval - self.assertFalse('Timeout' in fake_out.getvalue()) + self.assertTrue((end_time - start_time) >= 3) # time_expect = (count - 1) * interval + self.assertNotIn('Timeout', fake_out.getvalue()) @unittest.skipUnless(sys.platform == 'linux', "Linux only") def test_interface(self): @@ -85,9 +85,10 @@ def test_interface(self): def test_src_addr(self): with patch("sys.stdout", new=io.StringIO()) as fake_out: my_ip = socket.gethostbyname(socket.gethostname()) - dest_addr = "example.com" - if my_ip == "127.0.0.1" or my_ip == "127.0.1.1": # This may caused by /etc/hosts settings. + if my_ip in ("127.0.0.1", "127.0.1.1"): # This may caused by /etc/hosts settings. dest_addr = my_ip # only localhost can send and receive from 127.0.0.1 (or 127.0.1.1 on Ubuntu). + else: + dest_addr = "example.com" command_line.main(['-S', my_ip, dest_addr]) self.assertRegex(fake_out.getvalue(), r".*[0-9]+ms.*") @@ -97,9 +98,8 @@ def test_debug(self): self.assertIn("[DEBUG]", fake_err.getvalue()) def test_exceptions(self): - with patch("sys.stdout", new=io.StringIO()) as fake_out: - with self.assertRaises(errors.Timeout): - command_line.main(['--exceptions', '-t', '0.0001', 'example.com']) + with self.assertRaises(errors.Timeout): + command_line.main(['--exceptions', '-t', '0.0001', 'example.com']) if __name__ == "__main__": diff --git a/tests/test_ping3.py b/tests/test_ping3.py index 1c9b9b9..5508328 100644 --- a/tests/test_ping3.py +++ b/tests/test_ping3.py @@ -170,13 +170,12 @@ def test_verbose_ping_count(self): def test_verbose_ping_interval(self): with patch("sys.stdout", new=io.StringIO()) as fake_out: delay = ping3.ping("example.com") - self.assertIsInstance(delay, float) self.assertTrue(0 < delay < 0.75) # If interval does not work, the total delay should be < 3s (4 * 0.75s) start_time = time.time() ping3.verbose_ping("example.com", interval=1) # If interval does work, the total delay should be > 3s (3 * 1s) end_time = time.time() self.assertTrue((end_time - start_time) >= 3) # time_expect = (count - 1) * interval - self.assertRegex(fake_out.getvalue(), r".*[0-9]+.*") + self.assertNotIn('Timeout', fake_out.getvalue()) # Ensure no timeout def test_DEBUG(self): with patch("ping3.DEBUG", True), patch("sys.stderr", new=io.StringIO()):