관리 메뉴

솜씨좋은장씨

[Python] Pycharm에서 디버깅하기! 본문

Programming/Python

[Python] Pycharm에서 디버깅하기!

솜씨좋은장씨 2020. 4. 4. 09:04
728x90
반응형

 

[Python] Pycharm에서 unittest 사용해보기!

Pycharm에서 unittest를 사용하는 방법에 대해서 정리해보려합니다. Step 3. Test your first Python application - Help | PyCharm Step 3. Test your first Python application Remember, in the first tutoria..

somjang.tistory.com

지난 글에서 테스트를 하면서 오류났던 부분을 디버깅을 통해서 고쳐보려합니다.

Testing started at 5:53 오후 ...
/Users/donghyunjang/PycharmProjects/MyTestPractice/venv/bin/python "/Applications/PyCharm CE.app/Contents/helpers/pycharm/_jb_unittest_runner.py" --path /Users/donghyunjang/PycharmProjects/MyTestPractice/test_car.py
Launching unittests with arguments python -m unittest /Users/donghyunjang/PycharmProjects/MyTestPractice/test_car.py in /Users/donghyunjang/PycharmProjects/MyTestPractice



0 != -15

Expected :-15
Actual   :0
<Click to see difference>

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals
    old(self, first, second, msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 839, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 832, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: -15 != 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 615, in run
    testMethod()
  File "/Users/donghyunjang/PycharmProjects/MyTestPractice/test_car.py", line 52, in test_multiple_brakes_at_zero
    self.assertEqual(self.car.speed, 0)



Ran 9 tests in 0.034s

FAILED (failures=2)


0 != -5

Expected :-5
Actual   :0
<Click to see difference>

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals
    old(self, first, second, msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 839, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 832, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: -5 != 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 615, in run
    testMethod()
  File "/Users/donghyunjang/PycharmProjects/MyTestPractice/test_car.py", line 47, in test_should_not_allow_negative_speed
    self.assertEqual(self.car.speed, 0)


Process finished with exit code 1

Assertion failed

Assertion failed

Assertion failed

 

1. 브레이크 포인트 설정하기

출력결과를 확인하고 필요한 부분에 브레이크 포인트를 설정합니다.

 

2. 디버깅하기

def test_should_not_allow_negative_speed(self): 좌측에 있는 화살표를 클릭하고

Debug "Unittests for test_c..."를 클릭합니다.

다음 버튼을 클릭하면서 각 과정별로 값이 바뀌는 것을 확인하면서 어느 부분을 수정하면 좋을지 확인합니다.

 

 

 

여기서 문제는 속도가 음수인 -5가 되는 것입니다.

 

Car.py

... 생략

def brake(self):
    if self.speed < 5:
        self.speed = 0
    else:
        self.speed -= 5
        
... 생략

 

해당 부분을 수정하면 모든 테스트에 대해서 결과가 이상없는 것을 확인할 수 있습니다.

Comments