SlideShare a Scribd company logo
PYTHON TESTING
John
Saturday, December 21, 2013
Type of testing
• Unit testing: Unit testing is testing of the
smallest possible pieces of a program. it's the
foundation upon which everything else is
based
• Integration testing: tests encompass
interactions between related units.
• System testing: system tests are an extreme
form of integration tests.
UNITTEST MODULE
unittest introduction
• the batteries-included test module standard
library.
• Similar usage as JUnit, nUnit, CppUnit series of
tools.
Unittest package
Import unittest
From unittest import TestCase,main
class two_failing_tests(TestCase):
def test_assertTrue(self):
self.assertTrue(1 == 1 + 1)
def test_assertEqual(self):
self.assertEqual(1, 1 + 1)
if __name__ == '__main__':
main()
Assert method
•
•
•
•

assertTrue: will succeed if expression is true
assertFalse: will succeed if expression is false
assertEqual, assertNotEqual
assertAlmostEqual: use this one compare
comparing floating point numbers. For example
3==3.00
• assertNotAlmostEqual
• assertRaises
• Final one: fail
PART 1:
INTRODUCE
DOCTEST MODULE
Doctest: the easiest Testing tool
• Doctest will be the mainstay of your testing
toolkit
• doctest tests are written in plain text.
• Doctest extracts the tests and ignores the
rest of the text
• So the tests can be embedded in humanreadable explanations or discussions.
creating and running your first
doctest
• Open a new text file in your editor, and name it test.txt.
• Insert the following text into the file:
This is a simple doctest that checks some of Python's arithmetic
operations.
>>> 2 + 2
4
>>> 3 * 3
10
• Run command: python -m doctest test.txt
• Or you can write a small python code file:
import doctest
doctest.testfile(“test.txt")
• You can simply add IDLE input and output as test here
Directives:
• +SKIP to skip one test
>>> 'This test would fail.' # doctest: +SKIP
• +ELLIPSIS: can use … match any substring in
the actual output
func(56, "hello") # doctest: +ELLIPSIS
<mocker.Mock object at ...>
• More directives see here
Embedding doctests in Python
docstrings
def testable(x):
r"""
The `testable` function returns the square root of its
parameter, or 3, whichever is larger.
>>> testable(7)
3.0
>>> testable(16)
4.0
>>> testable(9)
3.0
>>> testable(10) == 10 ** 0.5
True
"""
if x < 9:
return 3.0
return x ** 0.5

•Run command line: python ‑ m doctest ‑ v test.py
•If you want to run the test in code. Add cose like:
if __name__ == "__main__":
import doctest
doctest.testmod()
Tips
• Write your test before code development
• reload(module) if your module is changed.
reload array
PART 2: INTRO TO
PYTHON MOCKER
AND UNITTEST
Install mocker first
• pip install mocker
• Or python easy_install.py mocker
• Or download it by yourself
http://labix.org/mocker
What is Mock object?
• Mock objects imitate the real objects that
make up your program
• So we can decouple the multiplication class
from others
Step by step
1. Create the mocking context: mocker =
Mocker().
2. Create mocking object under this context: a =
mocker.mock()
3. demonstrate how you expect the mock objects
to be used :
func(56, "hello") # doctest: +ELLIPSIS
<mocker.Mock object at ...>
>>> mocker.result(11)

4. Here we expect func(56,”hello”) will output 11
6. Replay mode: mocker.replay(). Once enter
this mode, the first time call
func(56,”hello”). It will return 11 instead of
call this func.
7. mocker.restore() : back the point not set
mock.
8. mocker.verify(): check that the actual usage
of the mocks was as expected. For example:
check if func(56,”hello”) is 11
Function test
Parameter name in Mock
• ANY: any single object.i.e.func(a)
• ARGS: any more arguments. i.e. func(a,b)
• KWARGS: any more keyword arguments i.e.
func(a=1,b=2)
• IS: func(7, IS(param)) # doctest: +ELLIPSIS
• IN:func(7, IN([45, 68, 19])) # doctest:
+ELLIPSIS
CONTAINS: if the list contain this
vlaue
>>> from mocker import Mocker, CONTAINS
>>> mocker = Mocker()
>>> func = mocker.mock()
>>> func(7, CONTAINS(45)) # doctest: +ELLIPSIS
<mocker.Mock object at ...>
>>> mocker.result(5)
>>> mocker.replay()
>>> func(7, [12, 31, 45, 18])
5
>>> mocker.restore()
>>> mocker.verify()
MATCH: if match,it return true
>>> from mocker import Mocker, MATCH
>>> def is_odd(val):
... return val % 2 == 1
>>> mocker = Mocker()
>>> func = mocker.mock()
>>> func(7, MATCH(is_odd)) # doctest: +ELLIPSIS
<mocker.Mock object at ...>
>>> mocker.result(5)
>>> mocker.replay()
>>> func(7, 1001)
5
>>> mocker.restore()
>>> mocker.verify()
mocker.count
• Mocker.count to specify the expected
number of repetitions
• count(3): repeat 3 times
• count(1,3): repeat times is between 1 and 3.
• count(1,None): repeat at least 1 time, no
maximum.
Package test
• Use replace to temporarily replace the lib
from time import time
>>> from mocker import Mocker
>>> mocker = Mocker()
>>> mock_time = mocker.replace('time.time')
>>> mock_time() # doctest: +ELLIPSIS
<mocker.Mock object at ...>
>>> mocker.result(1.3)
>>> mocker.replay()
>>> '%1.3g' % time()
'1.3‘
>>> mocker.restore()
>>> mocker.verify()
Class test
• Let self be a mock object
>>> from testable import testable
>>> from mocker import Mocker
>>> mocker = Mocker()
>>> target = mocker.mock()
>>> target.method1(12) # doctest: +ELLIPSIS
<mocker.Mock object at ...>
>>> mocker.result(5)
>>> target.method2(12) # doctest: +ELLIPSIS
<mocker.Mock object at ...>
>>> mocker.result(7)
PART 3: PYTHON
TOOL NOSE
What is nose?
•
•
•
•

A tool for finding and running all of your tests
presents with a nice report after tests finish.
Nose understands doctest and unittest tests
Install nose from PYPI (use easy_install or
pip)
• After install it, run command line: nosetests
Recommendation how to organize
the source code folder
• Nose recognizes test files based on their names
– Any file whose name
contains test or Test contain
unittest TestCases
– Nose find doctest tests either
embedded in docstrings or
separate test files

• Reorgnize your folder. Change
to the dir. Run command line:
nosetests --with-doctest
--doctest-extension=txt -v
Options for nosetests
• Create a configuration file called nose.cfg or
.noserc in $HOME dir
(For windows, the $HOME means C:Usersusers)

• Place following inside it:
[nosetests]
with-doctest=1
doctest-extension=txt
include="(?:^[Dd]oc)“

• Run nosetests –v.
PART 4: TESTING
WEB APPLICATION
FRONTENDS USING
TWILL
Brief intro
• twill allows users to browse the Web from a
command-line interface.
• twill supports automated Web testing
• Twill has a simple Python interface.
• Use pip install twill package first
• Use command: twill-sh to start the shell
A simple example
• Create slashdot.twill file contain code:
code 200
follow Science
code 200
formvalue 2 fhfilter "aardvark"
submit
code 200
find aardvark code 200
follow Science
code 200
formvalue 2 fhfilter "aardvark"
submit
code 200
find aardvark

• Run commandl line: twill-sh -u http://slashdot.org/
slashdot.twill
You can also test interactively
•
•
•
•

>> go http://slashdot.org/
>> code 200
>> follow Science
….
Use twill in python
Import twill
browser = twill.get_browser()
Browser methods include: go,
reload,back,get_code,get_html,get_title,get_ur
l,find_link,follow_link,set_agent_string,get_all_
forms,get_form,get_form_field,clicked,submit,
save_cookies,load_cookies,clear_cookies
PART 5: OTHER
TESTING TOOLS
AND TECHNIQUES
Code coverage
• A code coverage keeps track of which lines of
code are (and aren't) executed while tests
are running
• Give you a report describing how well your
tests cover the whole body of code
Attention: Code coverage is a tool to give you insight
into what your tests are doing, and what they may
be overlooking. It's not the definition of a good test
suite.
1. Install PYPI package coverage (pip install
coverage)
2. When run your nose. Use option –withcoverage –-cover-erage

3. From the example, we saw line 16,19-20 of
toy.py do not executes.
Automated continuous integration
• automated continuous integration system
compiles your code (if need be) and runs
your tests many times, in many different
environments.
• Buildbot is a popular automated continuous
integration tool.
Reference
• Book << Python Testing:Beginner's Guide>>

More Related Content

What's hot

Python testing using mock and pytest
Python testing using mock and pytestPython testing using mock and pytest
Python testing using mock and pytest
Suraj Deshmukh
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded c
Benux Wei
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
Arulalan T
 
Modern Python Testing
Modern Python TestingModern Python Testing
Modern Python Testing
Alexander Loechel
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
Humberto Marchezi
 
Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentals
cbcunc
 
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit TestingPython Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
P3 InfoTech Solutions Pvt. Ltd.
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG
Greg.Helton
 
Introduzione al TDD
Introduzione al TDDIntroduzione al TDD
Introduzione al TDD
Andrea Francia
 
Testes pythonicos com pytest
Testes pythonicos com pytestTestes pythonicos com pytest
Testes pythonicos com pytest
viniciusban
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
Abner Chih Yi Huang
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
dn
 
PHPUnit
PHPUnitPHPUnit
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
Hector Canto
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008
Andrea Francia
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtest
Will Shen
 
Py.test
Py.testPy.test
Py.test
soasme
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
Mindfire Solutions
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
nicobn
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnit
Greg.Helton
 

What's hot (20)

Python testing using mock and pytest
Python testing using mock and pytestPython testing using mock and pytest
Python testing using mock and pytest
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded c
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
Modern Python Testing
Modern Python TestingModern Python Testing
Modern Python Testing
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentals
 
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit TestingPython Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG
 
Introduzione al TDD
Introduzione al TDDIntroduzione al TDD
Introduzione al TDD
 
Testes pythonicos com pytest
Testes pythonicos com pytestTestes pythonicos com pytest
Testes pythonicos com pytest
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtest
 
Py.test
Py.testPy.test
Py.test
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnit
 

Similar to Python testing

Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittest
Fariz Darari
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)
Fariz Darari
 
How to fake_properly
How to fake_properlyHow to fake_properly
How to fake_properly
Rainer Schuettengruber
 
UPC Testing talk 2
UPC Testing talk 2UPC Testing talk 2
UPC Testing talk 2
Timo Stollenwerk
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
Hans Jones
 
Test driven development
Test driven developmentTest driven development
Test driven development
christoforosnalmpantis
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
Ravikiran J
 
Testing Workshop
Testing WorkshopTesting Workshop
Testing Workshop
michaeldunstan
 
Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023
Mark Niebergall
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
Roman Okolovich
 
Testing My Patience
Testing My PatienceTesting My Patience
Testing My Patience
Adam Lowry
 
Python unit testing
Python unit testingPython unit testing
Python unit testing
Darryl Sherman
 
Mastering PowerShell Testing with Pester
Mastering PowerShell Testing with PesterMastering PowerShell Testing with Pester
Mastering PowerShell Testing with Pester
Mark Wragg
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Robot Media
 
Asp netmvc e03
Asp netmvc e03Asp netmvc e03
Asp netmvc e03
Yu GUAN
 
Making the most of your Test Suite
Making the most of your Test SuiteMaking the most of your Test Suite
Making the most of your Test Suite
ericholscher
 
Commencer avec le TDD
Commencer avec le TDDCommencer avec le TDD
Commencer avec le TDD
Eric Hogue
 
Mutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekMutation testing Bucharest Tech Week
Mutation testing Bucharest Tech Week
Paco van Beckhoven
 
Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013
Svetlin Nakov
 
Unit testing by Svetlin Nakov
Unit testing by Svetlin NakovUnit testing by Svetlin Nakov
Unit testing by Svetlin Nakov
it-tour
 

Similar to Python testing (20)

Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittest
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)
 
How to fake_properly
How to fake_properlyHow to fake_properly
How to fake_properly
 
UPC Testing talk 2
UPC Testing talk 2UPC Testing talk 2
UPC Testing talk 2
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
 
Testing Workshop
Testing WorkshopTesting Workshop
Testing Workshop
 
Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Testing My Patience
Testing My PatienceTesting My Patience
Testing My Patience
 
Python unit testing
Python unit testingPython unit testing
Python unit testing
 
Mastering PowerShell Testing with Pester
Mastering PowerShell Testing with PesterMastering PowerShell Testing with Pester
Mastering PowerShell Testing with Pester
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
 
Asp netmvc e03
Asp netmvc e03Asp netmvc e03
Asp netmvc e03
 
Making the most of your Test Suite
Making the most of your Test SuiteMaking the most of your Test Suite
Making the most of your Test Suite
 
Commencer avec le TDD
Commencer avec le TDDCommencer avec le TDD
Commencer avec le TDD
 
Mutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekMutation testing Bucharest Tech Week
Mutation testing Bucharest Tech Week
 
Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013
 
Unit testing by Svetlin Nakov
Unit testing by Svetlin NakovUnit testing by Svetlin Nakov
Unit testing by Svetlin Nakov
 

More from John(Qiang) Zhang

Git and github introduction
Git and github introductionGit and github introduction
Git and github introduction
John(Qiang) Zhang
 
Profiling in python
Profiling in pythonProfiling in python
Profiling in python
John(Qiang) Zhang
 
Introduction to jython
Introduction to jythonIntroduction to jython
Introduction to jython
John(Qiang) Zhang
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
John(Qiang) Zhang
 
A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modulesPython advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modules
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – application building blocks
Python advanced 3.the python std lib by example – application building blocksPython advanced 3.the python std lib by example – application building blocks
Python advanced 3.the python std lib by example – application building blocks
John(Qiang) Zhang
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithm
John(Qiang) Zhang
 

More from John(Qiang) Zhang (11)

Git and github introduction
Git and github introductionGit and github introduction
Git and github introduction
 
Profiling in python
Profiling in pythonProfiling in python
Profiling in python
 
Introduction to jython
Introduction to jythonIntroduction to jython
Introduction to jython
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
 
A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
 
Python advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modulesPython advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modules
 
Python advanced 3.the python std lib by example – application building blocks
Python advanced 3.the python std lib by example – application building blocksPython advanced 3.the python std lib by example – application building blocks
Python advanced 3.the python std lib by example – application building blocks
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithm
 

Recently uploaded

RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 

Recently uploaded (20)

RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 

Python testing

  • 2. Type of testing • Unit testing: Unit testing is testing of the smallest possible pieces of a program. it's the foundation upon which everything else is based • Integration testing: tests encompass interactions between related units. • System testing: system tests are an extreme form of integration tests.
  • 4. unittest introduction • the batteries-included test module standard library. • Similar usage as JUnit, nUnit, CppUnit series of tools.
  • 5. Unittest package Import unittest From unittest import TestCase,main class two_failing_tests(TestCase): def test_assertTrue(self): self.assertTrue(1 == 1 + 1) def test_assertEqual(self): self.assertEqual(1, 1 + 1) if __name__ == '__main__': main()
  • 6. Assert method • • • • assertTrue: will succeed if expression is true assertFalse: will succeed if expression is false assertEqual, assertNotEqual assertAlmostEqual: use this one compare comparing floating point numbers. For example 3==3.00 • assertNotAlmostEqual • assertRaises • Final one: fail
  • 8. Doctest: the easiest Testing tool • Doctest will be the mainstay of your testing toolkit • doctest tests are written in plain text. • Doctest extracts the tests and ignores the rest of the text • So the tests can be embedded in humanreadable explanations or discussions.
  • 9. creating and running your first doctest • Open a new text file in your editor, and name it test.txt. • Insert the following text into the file: This is a simple doctest that checks some of Python's arithmetic operations. >>> 2 + 2 4 >>> 3 * 3 10 • Run command: python -m doctest test.txt • Or you can write a small python code file: import doctest doctest.testfile(“test.txt") • You can simply add IDLE input and output as test here
  • 10. Directives: • +SKIP to skip one test >>> 'This test would fail.' # doctest: +SKIP • +ELLIPSIS: can use … match any substring in the actual output func(56, "hello") # doctest: +ELLIPSIS <mocker.Mock object at ...> • More directives see here
  • 11. Embedding doctests in Python docstrings def testable(x): r""" The `testable` function returns the square root of its parameter, or 3, whichever is larger. >>> testable(7) 3.0 >>> testable(16) 4.0 >>> testable(9) 3.0 >>> testable(10) == 10 ** 0.5 True """ if x < 9: return 3.0 return x ** 0.5 •Run command line: python ‑ m doctest ‑ v test.py •If you want to run the test in code. Add cose like: if __name__ == "__main__": import doctest doctest.testmod()
  • 12. Tips • Write your test before code development • reload(module) if your module is changed. reload array
  • 13. PART 2: INTRO TO PYTHON MOCKER AND UNITTEST
  • 14. Install mocker first • pip install mocker • Or python easy_install.py mocker • Or download it by yourself http://labix.org/mocker
  • 15. What is Mock object? • Mock objects imitate the real objects that make up your program • So we can decouple the multiplication class from others
  • 16. Step by step 1. Create the mocking context: mocker = Mocker(). 2. Create mocking object under this context: a = mocker.mock() 3. demonstrate how you expect the mock objects to be used : func(56, "hello") # doctest: +ELLIPSIS <mocker.Mock object at ...> >>> mocker.result(11) 4. Here we expect func(56,”hello”) will output 11
  • 17. 6. Replay mode: mocker.replay(). Once enter this mode, the first time call func(56,”hello”). It will return 11 instead of call this func. 7. mocker.restore() : back the point not set mock. 8. mocker.verify(): check that the actual usage of the mocks was as expected. For example: check if func(56,”hello”) is 11
  • 19. Parameter name in Mock • ANY: any single object.i.e.func(a) • ARGS: any more arguments. i.e. func(a,b) • KWARGS: any more keyword arguments i.e. func(a=1,b=2) • IS: func(7, IS(param)) # doctest: +ELLIPSIS • IN:func(7, IN([45, 68, 19])) # doctest: +ELLIPSIS
  • 20. CONTAINS: if the list contain this vlaue >>> from mocker import Mocker, CONTAINS >>> mocker = Mocker() >>> func = mocker.mock() >>> func(7, CONTAINS(45)) # doctest: +ELLIPSIS <mocker.Mock object at ...> >>> mocker.result(5) >>> mocker.replay() >>> func(7, [12, 31, 45, 18]) 5 >>> mocker.restore() >>> mocker.verify()
  • 21. MATCH: if match,it return true >>> from mocker import Mocker, MATCH >>> def is_odd(val): ... return val % 2 == 1 >>> mocker = Mocker() >>> func = mocker.mock() >>> func(7, MATCH(is_odd)) # doctest: +ELLIPSIS <mocker.Mock object at ...> >>> mocker.result(5) >>> mocker.replay() >>> func(7, 1001) 5 >>> mocker.restore() >>> mocker.verify()
  • 22. mocker.count • Mocker.count to specify the expected number of repetitions • count(3): repeat 3 times • count(1,3): repeat times is between 1 and 3. • count(1,None): repeat at least 1 time, no maximum.
  • 23. Package test • Use replace to temporarily replace the lib from time import time >>> from mocker import Mocker >>> mocker = Mocker() >>> mock_time = mocker.replace('time.time') >>> mock_time() # doctest: +ELLIPSIS <mocker.Mock object at ...> >>> mocker.result(1.3) >>> mocker.replay() >>> '%1.3g' % time() '1.3‘ >>> mocker.restore() >>> mocker.verify()
  • 24. Class test • Let self be a mock object >>> from testable import testable >>> from mocker import Mocker >>> mocker = Mocker() >>> target = mocker.mock() >>> target.method1(12) # doctest: +ELLIPSIS <mocker.Mock object at ...> >>> mocker.result(5) >>> target.method2(12) # doctest: +ELLIPSIS <mocker.Mock object at ...> >>> mocker.result(7)
  • 26. What is nose? • • • • A tool for finding and running all of your tests presents with a nice report after tests finish. Nose understands doctest and unittest tests Install nose from PYPI (use easy_install or pip) • After install it, run command line: nosetests
  • 27. Recommendation how to organize the source code folder • Nose recognizes test files based on their names – Any file whose name contains test or Test contain unittest TestCases – Nose find doctest tests either embedded in docstrings or separate test files • Reorgnize your folder. Change to the dir. Run command line: nosetests --with-doctest --doctest-extension=txt -v
  • 28. Options for nosetests • Create a configuration file called nose.cfg or .noserc in $HOME dir (For windows, the $HOME means C:Usersusers) • Place following inside it: [nosetests] with-doctest=1 doctest-extension=txt include="(?:^[Dd]oc)“ • Run nosetests –v.
  • 29. PART 4: TESTING WEB APPLICATION FRONTENDS USING TWILL
  • 30. Brief intro • twill allows users to browse the Web from a command-line interface. • twill supports automated Web testing • Twill has a simple Python interface. • Use pip install twill package first • Use command: twill-sh to start the shell
  • 31. A simple example • Create slashdot.twill file contain code: code 200 follow Science code 200 formvalue 2 fhfilter "aardvark" submit code 200 find aardvark code 200 follow Science code 200 formvalue 2 fhfilter "aardvark" submit code 200 find aardvark • Run commandl line: twill-sh -u http://slashdot.org/ slashdot.twill
  • 32. You can also test interactively • • • • >> go http://slashdot.org/ >> code 200 >> follow Science ….
  • 33. Use twill in python Import twill browser = twill.get_browser() Browser methods include: go, reload,back,get_code,get_html,get_title,get_ur l,find_link,follow_link,set_agent_string,get_all_ forms,get_form,get_form_field,clicked,submit, save_cookies,load_cookies,clear_cookies
  • 34. PART 5: OTHER TESTING TOOLS AND TECHNIQUES
  • 35. Code coverage • A code coverage keeps track of which lines of code are (and aren't) executed while tests are running • Give you a report describing how well your tests cover the whole body of code Attention: Code coverage is a tool to give you insight into what your tests are doing, and what they may be overlooking. It's not the definition of a good test suite.
  • 36. 1. Install PYPI package coverage (pip install coverage) 2. When run your nose. Use option –withcoverage –-cover-erage 3. From the example, we saw line 16,19-20 of toy.py do not executes.
  • 37. Automated continuous integration • automated continuous integration system compiles your code (if need be) and runs your tests many times, in many different environments. • Buildbot is a popular automated continuous integration tool.
  • 38. Reference • Book << Python Testing:Beginner's Guide>>

Editor's Notes

  1. This template can be used as a starter file for presenting training materials in a group setting. Sections Right-click on a slide to add sections. Sections can help to organize your slides or facilitate collaboration between multiple authors. Notes Use the Notes section for delivery notes or to provide additional details for the audience. View these notes in Presentation View during your presentation. Keep in mind the font size (important for accessibility, visibility, videotaping, and online production) Coordinated colors Pay particular attention to the graphs, charts, and text boxes. Consider that attendees will print in black and white or grayscale. Run a test print to make sure your colors work when printed in pure black and white and grayscale. Graphics, tables, and graphs Keep it simple: If possible, use consistent, non-distracting styles and colors. Label all graphs and tables.