Difference between revisions of "Python-3.4.3/C4/Testing-and-Debugging/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''': '''Testing and debugging''' '''Author: Puneeth, Thirumalesh H S, Arun KP''' '''Keywords: Python, Ipython, testing, debugging, automate test, coding st...")
 
 
(One intermediate revision by one other user not shown)
Line 28: Line 28:
 
* Understand the need for '''coding''' '''style''' and
 
* Understand the need for '''coding''' '''style''' and
 
* Learn some of the '''standards''' followed by the '''Python''' '''Community'''
 
* Learn some of the '''standards''' followed by the '''Python''' '''Community'''
 
 
  
 
|-
 
|-
Line 41: Line 39:
 
* '''IPython 5.1.0''' and
 
* '''IPython 5.1.0''' and
 
* '''Gedit text editor'''
 
* '''Gedit text editor'''
 
 
  
 
|-
 
|-
Line 48: Line 44:
  
 
Prerequisite  
 
Prerequisite  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to use''' functions.'''
 
+
* use''' functions.'''
+
  
 
If not, see the relevant '''Python''' tutorials on this website.
 
If not, see the relevant '''Python''' tutorials on this website.
Line 100: Line 94:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the file find_gcd.py
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the file find_gcd.py
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the file as '''find''''' underscore '''''gcd.py '''in the current working '''directory'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the file as '''find''''' underscore '''''gcd.py '''in the current '''working directory'''.  
  
 
|-
 
|-
Line 137: Line 131:
  
  
Open the terminal and type  
+
Open the '''terminal''' and type  
  
  
Line 209: Line 203:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First we need '''import gcd''' function from '''find''''' underscore '''''gcd''' in order to use it for testing.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First we need '''import gcd function''' from '''find''''' underscore '''''gcd''' in order to use it for testing.
  
  
Line 215: Line 209:
  
  
The first two '''input''' '''parameters''' are assigned to the variables '''x '''and''' y. '''
+
The first two '''input parameters''' are assigned to the variables '''x '''and''' y. '''
  
  
Line 221: Line 215:
  
  
We check whether the value returned by the '''gcd function '''is equal to the value in the variable''' result.'''
+
We check whether the value returned by the '''gcd function '''is equal to the value in the '''variable result.'''
  
  
Line 238: Line 232:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Point to the output
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Point to the output
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| As you can see, all the three test cases in '''testcases.txt''' are passed.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| As you can see, all the three '''test cases''' in '''testcases.txt''' are passed.
  
  
The value calculated by the gcd function is equal to the output value provided in the '''testcases.txt.'''
+
The value calculated by the '''gcd function''' is equal to the output value provided in the '''testcases.txt.'''
  
 
|-
 
|-
Line 254: Line 248:
  
 
Assignment 1  
 
Assignment 1  
 
 
 
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| For the same inputs as '''gcd''' write '''automated''' '''tests''' for '''LCM'''.  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| For the same inputs as '''gcd''' write '''automated''' '''tests''' for '''LCM'''.  
  
Line 264: Line 255:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution.
  
 
|-
 
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open the find_lcm.py
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open find_lcm.py
  
  
Line 286: Line 277:
  
  
Note that both these files should be in the current working directory.
+
Note that both these files should be in the current '''working directory'''.
  
  
Line 315: Line 306:
  
 
Coding Style
 
Coding Style
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| * A good '''program''' should be '''readable.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"|  
 +
* A good '''program''' should be '''readable.'''
 
* So others can extend and improve it.  
 
* So others can extend and improve it.  
 
* '''Code''' is read more often than it is written.  
 
* '''Code''' is read more often than it is written.  
 
 
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Show Slide
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Show Slide
  
Meaningfull names  
+
Meaningful names  
 
+
 
+
 
+
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We choose a name so that it becomes easier to understand its usage.  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We choose a name so that it becomes easier to understand its usage.  
  
  
Let’s look at this with an example.
+
As we can see in the example, it is very easy to understand what the '''code''' is doing.  
 
+
 
+
As we can see in the example,
+
 
+
it is very easy to understand what the '''code''' is doing.  
+
  
  
Line 350: Line 332:
 
* 79 characters limit on a line
 
* 79 characters limit on a line
 
* '''Functions''' and '''methods''' should be separated with two '''blank''' '''lines'''.  
 
* '''Functions''' and '''methods''' should be separated with two '''blank''' '''lines'''.  
 
 
  
 
|-
 
|-
Line 359: Line 339:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| * Use '''Docstring''' to document a specific segment of code.
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| * Use '''Docstring''' to document a specific segment of code.
 
* Use '''whitespace''' around '''operators''' and after punctuations.  
 
* Use '''whitespace''' around '''operators''' and after punctuations.  
 
 
  
 
|-
 
|-
Line 373: Line 351:
 
* '''Automate''' '''tests''' using many '''predefined''' '''test''' '''cases''' and
 
* '''Automate''' '''tests''' using many '''predefined''' '''test''' '''cases''' and
 
* Use '''python''' '''coding''' '''standards'''.  
 
* Use '''python''' '''coding''' '''standards'''.  
 
 
  
 
|-
 
|-
Line 382: Line 358:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here is a self assessment question for you to solve  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here is a self assessment question for you to solve  
  
# What is the proper indentation for python code according to the style guidelines?  
+
# What is the proper indentation for '''python code''' according to the '''style''' guidelines?  
 
+
 
+
  
 
|-
 
|-
Line 390: Line 364:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answer,  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answer,  
  
# '''Four''' '''Space''' '''Indentation''' is required for writing a python code according to the style guidelines.  
+
# '''Four Space Indentation''' is required for writing a '''python code''' according to the '''style''' guidelines.  
 
+
 
+
  
 
|-
 
|-

Latest revision as of 10:44, 2 November 2018

Title of script: Testing and debugging

Author: Puneeth, Thirumalesh H S, Arun KP

Keywords: Python, Ipython, testing, debugging, automate test, coding style, python community, video tutorial


Visual Cue Narration
Show Slide title Welcome to the spoken tutorial on Testing and debugging.
Show Slide

Objectives


In this tutorial, you will,
  • Understand what is software testing
  • Test simple functions for their functionality
  • Automate tests
  • Understand the need for coding style and
  • Learn some of the standards followed by the Python Community
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3
  • IPython 5.1.0 and
  • Gedit text editor
Show Slide

Prerequisite

To practise this tutorial, you should know how to use functions.

If not, see the relevant Python tutorials on this website.

Show Slide

Software Testing


First we will learn about software testing.


Software testing is the process to evaluate the functionality of a software or a program.


It helps to find whether the program met the specified requirements or not.


It ensures a defect free program so that we will get a quality program.

Show slide:

Source code


All the codes used in this tutorial are available in the Code Files link of this tutorial.


You should download in the current working directory and use them.

gcd function

def gcd(a, b): if b == 0: return a return gcd(b, a%b)

Highlight def gcd(a,b):

Open any text editor and type the following code.


This is a simple function to calculate gcd of two numbers.


We need a set of inputs for the variable a and b.

Save the file find_gcd.py Save the file as find underscore gcd.py in the current working directory.
Open the file test_gcd.py Next we will open the file test underscore gcd.py.
from find_gcd import gcd


if __name__ == '__main__':

result = gcd(48, 64)

if result != 16:

print("Test failed")

print("Test Passed")

Let our test case be 48 and 64 as a and b.

For this test case we know that the GCD is 16.


So that is the expected output.

[Terminal]


python3 test_gcd.py


Let us now run the script and test our code.


Open the terminal and type


python3 test underscore gcd.py


We get the output as Test Passed' which means our code is correct.

But there can be a number of cases where the gcd function might break.
Slide: Test cases So to check where our code is breaking we should run many tests.


This is where the concept of automating tests comes in.

Open the file testcases.txt and show Let us first try and automate tests on the gcd function.


Open the file textcases.txt where the various testing parameters are given.

Open testcases.txt

12 28 4

18 36 18

4678 39763 2339

The structure of the file will have two input parameters.


The third parameter is the correct output result.


We have separated the elements by a space.

Open automate_test_gcd.py Next let us open the file automate underscore test underscore gcd.py
Highlight the code:

from find_gcd import gcd


if __name__ == '__main__':

for line in open('testcases.txt'):

numbers = line.split()

x, y = int(numbers[0]) , int(numbers[1])

result = int(numbers[2])

if gcd(x, y) != result:

print ("Failed gcd test for", x, y)

else:

print ("Test passed", result)


First we need import gcd function from find underscore gcd in order to use it for testing.


Next the testcases.txt file is read line by line.


The first two input parameters are assigned to the variables x and y.


The third parameter which is the correct output value is assigned to the variable result.


We check whether the value returned by the gcd function is equal to the value in the variable result.


Finally it prints the message accordingly.

In the terminal, type


python3 automate_test_gcd.py

In the terminal, type


python3 automate underscore test underscore gcd.py

Point to the output As you can see, all the three test cases in testcases.txt are passed.


The value calculated by the gcd function is equal to the output value provided in the testcases.txt.

Pause the video.


Try this exercise and then resume the video.

Show Slide

Assignment 1

For the same inputs as gcd write automated tests for LCM.


Use the data from the file lcmtestcases.txt

Switch terminal Switch to the terminal for the solution.
Open find_lcm.py


Open lcmtestcases.txt


Let us see the code to calculate lcd of two numbers.


The file name is find underscore lcm.py


This is the data file for the lcm test cases.


This file name is lcmtestcases.txt


Note that both these files should be in the current working directory.


Let us now run the script and test our code.

Type python3 find_lcm.py Type python3 find underscore lcm.py
Open find_lcm.py and show.


[Terminal]


python3 find_lcm.py

Here, the third test case failed.


Because the corresponding input in lcmtestcases.txt is incorrect.


This is to check the behavior of the program on incorrect conditions.

Show Slide

Coding Style

  • A good program should be readable.
  • So others can extend and improve it.
  • Code is read more often than it is written.
Show Slide

Meaningful names

We choose a name so that it becomes easier to understand its usage.


As we can see in the example, it is very easy to understand what the code is doing.


Proper naming helps so much in understanding the code.

Show Slide

Coding Instructions

Also one should keep in mind the following things while writing a code in Python.
  • Four Space Indentation
  • 79 characters limit on a line
  • Functions and methods should be separated with two blank lines.
Show Slide

Code Instructions

* Use Docstring to document a specific segment of code.
  • Use whitespace around operators and after punctuations.
Show Slide

Summary

This brings us to the end of this tutorial. Let us summarize.

In this tutorial, we have learnt to,

  • Create simple tests for a function
  • Automate tests using many predefined test cases and
  • Use python coding standards.
Show Slide

Self assessment questions

Here is a self assessment question for you to solve
  1. What is the proper indentation for python code according to the style guidelines?
Show Slide Solutions And the answer,
  1. Four Space Indentation is required for writing a python code according to the style guidelines.
Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst