Difference between revisions of "Python-3.4.3/C4/Testing-and-Debugging/English"
Nancyvarkey (Talk | contribs) |
|||
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.''' |
− | + | ||
− | + | ||
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 | + | | 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''' | + | | 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 | + | 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 | + | 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 | + | | 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 | ||
− | + | 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. | ||
− | As we can see in the example, | + | As we can see in the example, it is very easy to understand what the '''code''' is doing. |
− | + | ||
− | it is very easy to understand what the '''code''' is doing. | + | |
Line 347: | 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 356: | 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 370: | 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 379: | 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 387: | 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 | + | # '''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,
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
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.
|
Show slide:
Source code
|
All the codes used in this tutorial are available in the Code Files link of this tutorial.
|
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.
|
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
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.
|
[Terminal]
|
Let us now run the script and test our code.
|
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.
|
Open the file testcases.txt and show | Let us first try and automate tests on the gcd function.
|
Open testcases.txt
12 28 4 18 36 18 4678 39763 2339 |
The structure of the file will have two input parameters.
|
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
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.
|
In the terminal, type
|
In the terminal, type
|
Point to the output | As you can see, all the three test cases in testcases.txt are passed.
|
Pause the video.
| |
Show Slide
Assignment 1 |
For the same inputs as gcd write automated tests for LCM.
|
Switch terminal | Switch to the terminal for the solution. |
Open find_lcm.py
|
Let us see the code to calculate lcd of two numbers.
|
Type python3 find_lcm.py | Type python3 find underscore lcm.py |
Open find_lcm.py and show.
|
Here, the third test case failed.
|
Show Slide
Coding Style |
|
Show Slide
Meaningful names |
We choose a name so that it becomes easier to understand its usage.
|
Show Slide
Coding Instructions |
Also one should keep in mind the following things while writing a code in Python.
|
Show Slide
Code Instructions |
* Use Docstring to document a specific segment of code.
|
Show Slide
Summary |
This brings us to the end of this tutorial. Let us summarize.
In this tutorial, we have learnt to,
|
Show Slide
Self assessment questions |
Here is a self assessment question for you to solve
|
Show Slide Solutions | And the answer,
|
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 |
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. |