in ,

Python Debugging and Testing – Day 8th Python Series

As we all know when we write a program, there are two types of errors that we might get. So we need to debug them, and that’s today’s topic: Python Debugging and Testing.

  1. Debugging syntax errors
  2. Debugging logical errors

New here ?

Learn python from scratch for free

1.Debugging syntax error in python

Eclipse detects the syntax errors while we type the code or when we try to run code.

It underlines the errors in the code.

debugging in python
python debugging

2. Debugging logical errors in python

Logical errors are hard to find since eclipse can not find logical errors. We have to find out logical errors by executing the code line by line.

Observe the output of this program

N =10
A = 30
B = 50
n = A+B
print(N)

Output – 10

You might be wondering that how can the value be 10, it should be (30+50) =80.

But you are wrong here.

These are the common mistakes most novice programmer does.

That is why we need to debug the program.

Let’s start debugging program by taking the example below

Example:

i=1
n=10
while(i<=n):
    if(i%2==0):
        print(i)
        i+=1
    else:
        continue
    i+=1

Follow the steps to debug the code:

  1. Copy the above code and paste it in Eclipse IDE
  2. Change the perspective to Debug, for that navigate to Window>perspective>open perspactive>debug
  3. Use the debugger tool in order to see what is happening in the program.

For that we want to break the execution at some point, for that double click on the line you want.

A green tick will be shown when you select a line.

  1. Click on the Debug button
python debugging
python debugging

This is the layout of the debug perspective below. You can now observe that the program has paused at line number 6, since we had created a breakpoint at that line.

You can go back to programming layout anytime by clicking on the Programming Assignment button on top right.

how to debug program in python
python debugging

Let’s observe the program below. The top right portion of the window has a tab called Variables. This shows a list of all variables and their current value. Click on a variable name to see its value displayed below it.

debugging syntax error in python

If you want to execute one line at a time from the breakpoint, you can use the step over function.

As we continue to step over we observe that control has gone back to line 6. At line 6, value of “i” is still 1. Continue to step over once again and observe the value of “i”. We observe that value of “i” remains as 1, thereby resulting in infinte loop

python debugging
python tutorial

Now we got the Bug, we can now fix our code.

Modified code:

i=1
n=10
while(i<=n):
    if(i%2==0):
        print(i)
    i+=1

Testing code using  Python PyTest test cases

If we have to write test cases and test a program written for the above requirement, instead of writing the test cases and testing it manually, we can use some tools.

pytest can be used to write test cases and automatically test a program.

Once the test cases are written, we need to execute those test cases against the program to identify the test cases that have passed or failed. If there are any failed test cases, we should debug the program for any logical errors, correct it and test again as shown below.

pytest in python

Let’s configure PyTest

  1. Create a pyDev project in Eclipse. Create two files, solution.py and test_solution.py inside a pyDev package in the project.
  1. To configure pytest, click on Run -> Run Configurations
python pytest
  1. Double click on Python unit test.
python test cases
  1. Choose the Main Tab and click on Browse to choose the Project.
python testing
  1. Choose your project

Then click on Argument

Enable “Override PyUnit preferences for this Launch?” and choose Pytest runner from the drop-down.

Enter the following text in the highlighted portion in the screenshot:

--tb=short 
--capture=no

Click apply then run.

That is it!

If you are still getting any problem in python debugging and testing then comment below with your problem we will surely reply within 12 hours.

FAQs

Frequently Asked Questions

How do you debug Python?

We have covered this topic scroll up to see in detail.

How do I enable breakpoints in eclipse?

To set a breakpoint double click on a line where you want to break execution.

What is debugger?

Debugging is the routine process of locating and removing bugs, errors or abnormalities from programs

Questions covered in this article.

  • What is usually the first step in debugging a program?
  • What are the debugging tools?
  • What is debugging and why is it important?
  • How do I turn off debug in eclipse?
  • How does debug work in Eclipse?
  • What is step over and step into?
  • How do I move to the next line while debugging in Eclipse?
  • How do you use a debugger?
  • How do I run debug mode?
  • HOW-TO: Debug Python Scripts with Eclipse 
  • Debugging Python Programs with Eclipse
  • How to test using PyTest
  • Testing sith Pytest
  • How to run multiple test cases using pytest.
  • How to automate python testing
  • How to automatically test python test cases
  • How to debugging and testing in python

You may also like these:

testing in pyton

What do you think?

Written by My Inquisitor

Comments

Leave a Reply

GIPHY App Key not set. Please check settings

Loading…

0

Collection Data types in Python – 7th Day of Python Series

Python Exception Handling -Try, Except and Finally (day 9th)