TESTING IN PYTHON ENVIRONMENT
Download
Report
Transcript TESTING IN PYTHON ENVIRONMENT
TESTING IN PYTHON
ENVIRONMENT
By Mr. Siva
STARTER
• In Pairs, Choose the starter activity sheet and spot the errors in code.
• Year 9 and 10 -- > White Card ( Green and Orange activity)
• Year 11 -- > Yellow Card (Orange and Red Activity)
• Make sure to justify the reason for the available error in a sentence.
( Literacy- Spelling and vocabulary will be checked)
Extension : Correct the errors.
Lesson Objectives
• To know: The importance of test driven development in python
environment.
• To create: A test case using unittest module
• To know: Different types of python testing methods
KEYWORDS : Source code, Test case, Unit testing, Methods, Objects ,Test file
Success Criteria
• To know the importance of test driven development
• To know the different methods of testing in python environment.
• Executing a test file to a python code.
KEYWORDS : Source code, Test case, Unit testing, Methods, Objects ,Test file
What is meant by testing ?
Why do we need it ?
"Testing", in general programming terms, is the practice of writing code (separate
from your actual application code) that invokes the code it tests to help determine
if there are any errors.
• Testing makes sure your code works properly under a given set of conditions
• Testing allows one to ensure that changes to the code did not break existing
functionality
• Testing forces one to think about the code under unusual conditions, possibly
revealing logical errors
• Good testing requires modular, decoupled code, which is a hallmark of good
system design
KEYWORDS : Source code, Test case, Unit testing, Methods, Objects ,Test file
Testing Methods
• The Basics
• Unittest
• Doctest
• Tools
•
•
•
•
•
py.test
Nose
tox
Unittest2
mock
Default test module in the Python standard
library
useful as an expressive documentation of the
main use cases of a module and its components.
Fully-featured and extensible test tool
Automatic test discovery to save you the hassle
of manually creating test suites
Tool for automating test environment
management
Improved API and better assertions
library for testing in Python
KEYWORDS : Source code, Test case, Unit test, Doctest, Methods, Objects ,Test file
Example – Factorial Program
1. import sys
2. def fact(n):
3. if n == 0:
4.
return 1
5. return n * fact(n -1)
6. def main(n):
7.
res = fact(n)
8.
print(res)
KEYWORDS : Source code, Test case, Unit test, Doctest, Methods, Objects ,Test file
Test Program for Factorial
1. import unittest
2. from factorial import fact
3. class TestFactorial(unittest.TestCase):
4.
def test_fact(self):
5.
res = fact(5)
6.
self.assertEqual(res, 120)
7. if __name__ == '__main__':
8.
unittest.main()
KEYWORDS : Source code, Test case, Unit test, Doctest, Methods, Objects ,Test file
Dry Run Testing
• A dry run is the process of a programmer manually working through
their code to trace the value of variables.
Different Assert Statement
KEYWORDS : Source code, Test case, Unit test, Doctest, Methods, Objects ,Test file
PLENARY
LINK : b.socrative.com/login/student
Room Name : DCBA
KEYWORDS : Source code, Test case, Unit test, Doctest, Methods, Objects ,Test file
Teachers Question
What is meant by Unit testing ?
What is meant by Test driven Development ?
What have you learnt today?
A question about today’s lesson
you would like answered
Or an aim for next lesson
Two things I knew
already
Three new things I
have learnt
HAPPY CODING !!!
GREEN CARD
1. if spam == 42
print('Hello!')
2. if spam = 42:
print('Hello!')
3. print('Hello!')
print('Howdy!')
4. if spam == 42:
print('Hello!')
5. spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
GREEN CARD
1. if spam == 42
print('Hello!')
2. if spam = 42:
print('Hello!')
3. print('Hello!')
print('Howdy!')
4. if spam == 42:
print('Hello!')
5. spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
GREEN CARD
1. if spam == 42
print('Hello!')
2. if spam = 42:
print('Hello!')
3. print('Hello!')
print('Howdy!')
4. if spam == 42:
print('Hello!')
5. spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
GREEN CARD
1. if spam == 42
print('Hello!')
2. if spam = 42:
print('Hello!')
3. print('Hello!')
print('Howdy!')
4. if spam == 42:
print('Hello!')
5. spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
GREEN CARD
1. if spam == 42
print('Hello!')
2. if spam = 42:
print('Hello!')
3. print('Hello!')
print('Howdy!')
4. if spam == 42:
print('Hello!')
5. spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
GREEN CARD
1. if spam == 42
print('Hello!')
2. if spam = 42:
print('Hello!')
3. print('Hello!')
print('Howdy!')
4. if spam == 42:
print('Hello!')
5. spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
ORANGE CARD
1. numEggs = 12
print('I have ' + numEggs + '
eggs.')
4. spam = 'THIS IS IN LOWERCASE.'
spam = spam.lowerr()
2. myName = 'Al'
5. spam = ['cat', 'dog', 'mouse']
print('My name is ' + myName
print(spam[6])
+ . How are you?')
3. spam = Round(4.2)
ORANGE CARD
1. numEggs = 12
print('I have ' + numEggs + '
eggs.')
4. spam = 'THIS IS IN LOWERCASE.'
spam = spam.lowerr()
2. myName = 'Al'
5. spam = ['cat', 'dog', 'mouse']
print('My name is ' + myName
print(spam[6])
+ . How are you?')
3. spam = Round(4.2)
ORANGE CARD
1. numEggs = 12
print('I have ' + numEggs + '
eggs.')
4. spam = 'THIS IS IN LOWERCASE.'
spam = spam.lowerr()
2. myName = 'Al'
5. spam = ['cat', 'dog', 'mouse']
print('My name is ' + myName
print(spam[6])
+ . How are you?')
3. spam = Round(4.2)
ORANGE CARD
1. numEggs = 12
print('I have ' + numEggs + '
eggs.')
4. spam = 'THIS IS IN LOWERCASE.'
spam = spam.lowerr()
2. myName = 'Al'
5. spam = ['cat', 'dog', 'mouse']
print('My name is ' + myName
print(spam[6])
+ . How are you?')
3. spam = Round(4.2)
ORANGE CARD
1. numEggs = 12
print('I have ' + numEggs + '
eggs.')
4. spam = 'THIS IS IN LOWERCASE.'
spam = spam.lowerr()
2. myName = 'Al'
5. spam = ['cat', 'dog', 'mouse']
print('My name is ' + myName
print(spam[6])
+ . How are you?')
3. spam = Round(4.2)
ORANGE CARD
1. numEggs = 12
print('I have ' + numEggs + '
eggs.')
4. spam = 'THIS IS IN LOWERCASE.'
spam = spam.lowerr()
2. myName = 'Al'
5. spam = ['cat', 'dog', 'mouse']
print('My name is ' + myName
print(spam[6])
+ . How are you?')
3. spam = Round(4.2)
RED CARD
1. class = 'algebra'
2. spam = 0
spam += 42
eggs += 42
3. someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
4. spam = 0
spam++
5. class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()
RED CARD
1. class = 'algebra'
2. spam = 0
spam += 42
eggs += 42
3. someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
4. spam = 0
spam++
5. class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()
RED CARD
1. class = 'algebra'
2. spam = 0
spam += 42
eggs += 42
3. someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
4. spam = 0
spam++
5. class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()
RED CARD
1. class = 'algebra'
2. spam = 0
spam += 42
eggs += 42
3. someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
4. spam = 0
spam++
5. class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()
RED CARD
1. class = 'algebra'
2. spam = 0
spam += 42
eggs += 42
3. someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
4. spam = 0
spam++
5. class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()
RED CARD
1. class = 'algebra'
2. spam = 0
spam += 42
eggs += 42
3. someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
4. spam = 0
spam++
5. class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()