Python-3.4.3/C4/Testing-and-Debugging/English

From Script | Spoken-Tutorial
Revision as of 10:44, 2 November 2018 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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