11import json
22import os
3- import sys
4- from unittest .mock import patch , MagicMock
3+ from unittest .mock import MagicMock , patch
54
65import pytest
76
@@ -120,10 +119,10 @@ def test_set_logger_level(self):
120119 ping = Ping ()
121120 logger = ping .set_logger_level ("DEBUG" )
122121 assert logger .level == 10 # DEBUG level
123-
122+
124123 logger = ping .set_logger_level ("INFO" )
125124 assert logger .level == 20 # INFO level
126-
125+
127126 logger = ping .set_logger_level ("ERROR" )
128127 assert logger .level == 40 # ERROR level
129128
@@ -176,7 +175,7 @@ def test_ping_valid_ip(self, mock_popen):
176175 b""
177176 )
178177 mock_popen .return_value = mock_process
179-
178+
180179 ping = Ping (count = 1 )
181180 result = ping .ping ("8.8.8.8" )
182181 assert result is not None
@@ -208,13 +207,6 @@ def test_ping_different_counts(self):
208207 assert "-c 1" in ping1 .command or "-n 1" in ping1 .command
209208 assert "-c 10" in ping2 .command or "-n 10" in ping2 .command
210209
211- def test_ping_different_counts (self ):
212- """Test ping with different packet counts"""
213- ping1 = Ping (count = 1 )
214- ping2 = Ping (count = 10 )
215- assert "-c 1" in ping1 .command or "-n 1" in ping1 .command
216- assert "-c 10" in ping2 .command or "-n 10" in ping2 .command
217-
218210 def test_set_ping_windows_os (self ):
219211 """Test _set_ping for Windows OS"""
220212 ping = Ping (count = 5 )
@@ -237,26 +229,26 @@ def test_tcping_initialization(self):
237229
238230 def test_ping_with_timeout (self ):
239231 """Test ping with custom timeout"""
240- ping1 = Ping (timeout = 5 )
241- ping2 = Ping (timeout = 10 )
232+ Ping (timeout = 5 )
233+ Ping (timeout = 10 )
242234 # Timeout should be in the command for layer 4
243235 ping_l4 = Ping (layer = 4 , timeout = 5 )
244236 assert "-t" in ' ' .join (ping_l4 .command ) or "-t 5" in ping_l4 .command
245237
246238 def test_tcping_loss_percentage_calculation (self ):
247239 """Test tcping loss percentage calculation (line 78)"""
248240 from pingping .ping import Ping
249-
241+
250242 # Mock tcping output with 80% successful (which means 80% success rate)
251243 tcping_output = """
252244 Probing 192.168.1.1:80/tcp - Port is open
253245 Port is open
254246 Port is open
255247 Port is open
256-
248+
257249 Statistics: 4 probes sent, 80% successful
258250 """
259-
251+
260252 # Test tcping command to hit line 78
261253 # When command="tcping", line 78 calculates: 100 - percentage = 100 - 80 = 20% loss
262254 ping = Ping (command = "tcping" , layer = 4 )
@@ -267,113 +259,113 @@ def test_tcping_loss_percentage_calculation(self):
267259
268260class TestCLIFunctions :
269261 """Test CLI functions: run() and help()"""
270-
262+
271263 def test_run_with_valid_ip (self ):
272264 """Test run() function with valid IP address (lines 146-181)"""
273265 from pingping .ping import run
274-
266+
275267 with patch ('sys.argv' , ['pingping' , '8.8.8.8' ]):
276268 with patch .object (Ping , 'ping' , return_value = {'ip' : '8.8.8.8' }):
277269 result = run ()
278270 assert result is not None
279271 assert result ['ip' ] == '8.8.8.8'
280-
272+
281273 def test_run_with_help_flag (self ):
282274 """Test run() function with -h flag (lines 146-181)"""
283275 from pingping .ping import run
284-
276+
285277 with patch ('sys.argv' , ['pingping' , '-h' ]):
286278 with pytest .raises (SystemExit ) as exc_info :
287279 run ()
288280 assert exc_info .value .code == - 1
289-
281+
290282 def test_run_with_help_long_flag (self ):
291283 """Test run() function with --help flag (lines 146-181)"""
292284 from pingping .ping import run
293-
285+
294286 with patch ('sys.argv' , ['pingping' , '--help' ]):
295287 with pytest .raises (SystemExit ) as exc_info :
296288 run ()
297289 assert exc_info .value .code == - 1
298-
290+
299291 def test_run_with_tcp_flag (self ):
300292 """Test run() function with -l4 flag (lines 146-181)"""
301293 from pingping .ping import run
302-
294+
303295 with patch ('sys.argv' , ['pingping' , '-l4' , '192.168.1.1' ]):
304296 with patch .object (Ping , 'ping' , return_value = {'ip' : '192.168.1.1' }):
305297 result = run ()
306298 assert result is not None
307-
299+
308300 def test_run_with_web_flag (self ):
309301 """Test run() function with --web flag (lines 146-181)"""
310302 from pingping .ping import run
311-
303+
312304 with patch ('sys.argv' , ['pingping' , '--web' , '192.168.1.1' ]):
313305 with patch .object (Ping , 'ping' , return_value = {'ip' : '192.168.1.1' }):
314306 result = run ()
315307 assert result is not None
316-
308+
317309 def test_run_with_tcp_long_flag (self ):
318310 """Test run() function with --tcp flag (lines 146-181)"""
319311 from pingping .ping import run
320-
312+
321313 with patch ('sys.argv' , ['pingping' , '--tcp' , '192.168.1.1' ]):
322314 with patch .object (Ping , 'ping' , return_value = {'ip' : '192.168.1.1' }):
323315 result = run ()
324316 assert result is not None
325-
317+
326318 def test_run_with_http_flag (self ):
327319 """Test run() function with --http flag (lines 146-181)"""
328320 from pingping .ping import run
329-
321+
330322 with patch ('sys.argv' , ['pingping' , '--http' , '192.168.1.1' ]):
331323 with patch .object (Ping , 'ping' , return_value = {'ip' : '192.168.1.1' }):
332324 result = run ()
333325 assert result is not None
334-
326+
335327 def test_run_with_count_flag (self ):
336328 """Test run() function with -c flag (lines 146-181)"""
337329 from pingping .ping import run
338-
330+
339331 with patch ('sys.argv' , ['pingping' , '-c' , '10' , '8.8.8.8' ]):
340332 with patch .object (Ping , 'ping' , return_value = {'ip' : '8.8.8.8' }):
341333 result = run ()
342334 assert result is not None
343-
335+
344336 def test_run_with_count_long_flag (self ):
345337 """Test run() function with --count flag (lines 146-181)"""
346338 from pingping .ping import run
347-
339+
348340 with patch ('sys.argv' , ['pingping' , '--count' , '15' , '1.1.1.1' ]):
349341 with patch .object (Ping , 'ping' , return_value = {'ip' : '1.1.1.1' }):
350342 result = run ()
351343 assert result is not None
352-
344+
353345 def test_run_with_no_ip_address (self ):
354346 """Test run() function with invalid arguments - no valid IP (lines 146-181)"""
355347 from pingping .ping import run
356-
348+
357349 # When -c flag is used but no valid IP is provided, it will create a default Ping
358350 # and attempt to ping None, which returns None (not a SystemExit)
359351 with patch ('sys.argv' , ['pingping' , '--help' , 'invalid' ]):
360352 with pytest .raises (SystemExit ) as exc_info :
361353 run ()
362354 assert exc_info .value .code == - 1
363-
355+
364356 def test_run_with_no_arguments (self ):
365357 """Test run() function with no arguments (lines 146-181)"""
366358 from pingping .ping import run
367-
359+
368360 with patch ('sys.argv' , ['pingping' ]):
369361 with pytest .raises (SystemExit ) as exc_info :
370362 run ()
371363 assert exc_info .value .code == - 1
372-
364+
373365 def test_help_function (self ):
374366 """Test help() function (lines 185-189)"""
375367 from pingping .ping import help
376-
368+
377369 with pytest .raises (SystemExit ) as exc_info :
378370 help ()
379371 assert exc_info .value .code == - 1
0 commit comments