Hello! I think I'll practice debugging this time. Python Debugging Tips Python test code to try in the shortest time I'm just copying and trying, so please see here for details! !!
First debug with pdb. Below is the code used for debugging.
import pdb;
for ii in range(1, 21):
if ii % 15 == 0:
print('Takeuchi Tsuyoshi')
elif ii % 3 == 0:
print('Takeuchi')
elif ii % 5 == 0:
print('Tsuyoshi')
else:
print(ii)
Start it first.
python -m pdb filename
If you type the command "n", it will be executed line by line.
(Pdb) n
> /home/takeuchi/test.py(4)<module>()
-> for ii in range(1, 21):
(Pdb) n
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) n
> /home/takeuchi/test.py(7)<module>()
-
> elif ii % 3 == 0:
(Pdb)
Then, when you type the command "c", the process proceeds at once instead of line by line.
> /home/takeuchi/test.py(4)<module>()
-> for ii in range(1, 21):
(Pdb) c
Takeuchi
4
Tsuyoshi
Takeuchi
7
8
Takeuchi
Tsuyoshi
11
Takeuchi
13
14
Takeuchi Tsuyoshi
16
17
Takeuchi
19
Tsuyoshi
The program finished and will be restarted
> /home/takeuchi/test.py(1)<module>()
-> import pdb;
(Pdb)
You can specify a breakpoint with b and check the processing up to that point. As a trial, let's execute the processing up to the first branch of the if statement. It was output properly.
(Pdb) b 5
Breakpoint 1 at /home/takeuchi/test.py:5
(Pdb) c
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
1
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
2
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Takeuchi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
4
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Tsuyoshi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb)
Takeuchi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
c(Pdb) c
7
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
8
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Takeuchi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Tsuyoshi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
11
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Takeuchi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
13
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
14
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Takeuchi Tsuyoshi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb)
(Pdb) b 5, ii == 15
By doing so, the processing will stop only when ii is 15.
Next, I would like to run the test in the same way. This is also a copy, so if you want to know more details, please see Reference.
test2.py
import unittest
import test as tt
class FizzBuzzTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_normal(self):
self.assertEqual(1, tt.fizzbuzz(1))
def test_fizz(self):
self.assertEqual("Tsuyoshi", tt.fizzbuzz(3))
def test_buzz(self):
self.assertEqual("Takeuchi", tt.fizzbuzz(5))
def test_fizzbuzz(self):
self.assertEqual("Takeuchi Tsuyoshi", tt.fizzbuzz(15))
if __name__ == "__main__":
unittest.main()
def setUp (self)
and def tearDown (self)
seem to be functions that run at the timing of initialization / end.
ʻAssertEqual ()` checks if the first and second arguments have the same value.
And the following is the imported test.py.
test.py
def fizzbuzz(number):
if number % 15 == 0:
return "Takeuchi Tsuyoshi"
if number % 5 == 0:
return "Takeuchi"
if number % 3 == 0:
return "Tsuyoshi"
return number
if __name__ == "__main__":
for i in range(1, 101):
print(fizzbuzz(i))
The execution result is as follows! !!
❯ python test2.py
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
Python Debugging Tips Python test code to try in the shortest time
Recommended Posts