Movatterモバイル変換


[0]ホーム

URL:


1,347 views

Python Programming Essentials - M39 - Unit Testing

The document discusses unit testing in Python. It defines unit testing as testing individual units or components of code to determine if they work as intended. It covers key concepts like test cases, test fixtures, test suites, and test runners. It also provides examples of how to write unit tests in Python using the unittest module and concepts like assertions, setup and teardown methods.

Embed presentation

http://www.skillbrew.com/SkillbrewTalent brewed by theindustry itselfUnit TestingPavan Verma@YinYangPavanFounder, P3 InfoTech Solutions Pvt. Ltd.Python Programming Essentials
© SkillBrew http://skillbrew.comWhat is Unit Testing A software testing method by which individualunits of source code are tested to determine ifthey are fit for use. Unit testing is a software developmentprocess in which the smallest testable parts ofan application, called units, are individuallyand independently scrutinized for properoperation. Unit testing is often automated butit can also be done manually.
© SkillBrew http://skillbrew.comUnit Testing Glossary test fixture – A test fixture represents thepreparation needed to perform one or moretests, and any associate cleanup actions. Thismay involve, for example, creating temporary orproxy databases, directories, or starting a serverprocess.
© SkillBrew http://skillbrew.comUnit Testing Glossary (2) test case – A test case is the smallest unitof testing. It checks for a specific responseto a particular set of inputs. Python unittest module provides a baseclass, TestCase, which may be used tocreate new test cases.
© SkillBrew http://skillbrew.comUnit Testing Glossary (3) test suite – A test suite is a collection oftest cases, test suites, or both. It is used toaggregate tests that should be executedtogether.
© SkillBrew http://skillbrew.comUnit Testing Glossary (4) test runner – A test runner is a componentwhich orchestrates the execution of testsand provides the outcome to the user. The runner may use a graphical interface,a textual interface, or return a special valueto indicate the results of executing thetests.
© SkillBrew http://skillbrew.comUnittest module The unittest module started life as thethird-party module PyUnit. PyUnit was a Python port of JUnit, theJava unit testing framework.
© SkillBrew http://skillbrew.comProgram to test – primes.pydef is_prime(number):"""Return True if *number* is prime.""“for element in range(number):if number % element == 0:return Falsereturn True
© SkillBrew http://skillbrew.comBasic unit test exampleimport unittestfrom primes import is_primeclass PrimesTestCase(unittest.TestCase):"""Tests for `primes.py`.""“def test_is_five_prime(self):"""Is five successfully determined to beprime?""“self.assertTrue(is_prime(5))if __name__ == '__main__':unittest.main()
© SkillBrew http://skillbrew.comRunning a unit test$ python test_primes.pyE ======================================================================ERROR: test_is_five_prime (__main__.PrimesTestCase)----------------------------------------------------------------------Traceback (most recent call last):File "test_primes.py", line 8, in test_is_five_primeself.assertTrue(is_prime(5))File "/home/jknupp/code/github_code/blug_private/primes.py", line 4, inis_primeif number % element == 0: ZeroDivisionError: integer division or modulo by zero----------------------------------------------------------------------Ran 1 test in 0.000s
© SkillBrew http://skillbrew.comAssertionsMethod Checks thatassertEqual(a, b) a == bassertNotEqual(a, b) a != bassertTrue(x) bool(x) is TrueassertFalse(x) bool(x) is FalseassertIs(a, b) a is bassertIsNot(a, b) a is not bassertIsNone(x) x is NoneassertIsNotNone(x) x is not NoneassertIn(a, b) a in bassertNotIn(a, b) a not in bassertIsInstance(a, b) isinstance(a, b)assertNotIsInstance(a, b) not isinstance(a, b)
© SkillBrew http://skillbrew.comAssertions (2)Method Checks thatassertAlmostEqual(a, b) round(a-b, 7) == 0assertNotAlmostEqual(a, b) round(a-b, 7) != 0assertGreater(a, b) a > bassertGreaterEqual(a, b) a >= bassertLess(a, b) a < bassertLessEqual(a, b) a <= bassertRegexpMatches(s, r) r.search(s)assertNotRegexpMatches(s, r) not r.search(s)assertItemsEqual(a, b)sorted(a) == sorted(b) and works withunhashable objsassertDictContainsSubset(a, b) all the key/value pairs in a exist in b
© SkillBrew http://skillbrew.comsetUp and tearDown#!/usr/bin/pythonimport unittestclass FooTest(unittest.TestCase):"""Sample test case"""# preparing to testdef setUp(self):""" Setting up for the test """print "FooTest:setUp_:begin"## do something...print "FooTest:setUp_:end"# ending the test
© SkillBrew http://skillbrew.comsetUp and tearDown (2)def tearDown(self):"""Cleaning up after the test"""print "FooTest:tearDown_:begin" ## dosomething...print "FooTest:tearDown_:end" # test routine Adef testA(self):"""Test routine A"""print "FooTest:testA"# test routine Bdef testB(self):"""Test routine B"""print "FooTest:testB"
© SkillBrew http://skillbrew.comsetUp and tearDown (3)
© SkillBrew http://skillbrew.comUnittest features setupClass() / tearDownClass() Skip tests Expected failures
© SkillBrew http://skillbrew.comTesting Frameworks pytest Nose
© SkillBrew http://skillbrew.comCode coverage coveragehttp://nedbatchelder.com/code/coverage/
© SkillBrew http://skillbrew.comReferences https://docs.python.org/2/library/unittest.html http://pytest.org/ http://nose.readthedocs.org/ http://nedbatchelder.com/code/coverage/
Python Programming Essentials - M39 - Unit Testing

Recommended

PDF
Test Driven Development With Python
 
PPTX
Python Programming Essentials - M35 - Iterators & Generators
PPT
Python testing
PPTX
Python Programming Essentials - M27 - Logging module
PPTX
Python Programming Essentials - M21 - Exception Handling
PPTX
unittest in 5 minutes
PPTX
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
PDF
Python Unit Test
PPT
Test Driven Development with PHPUnit
PDF
Effective testing with pytest
PDF
Introduction to Unit Testing with PHPUnit
PDF
Modern Python Testing
PPT
Advanced PHPUnit Testing
PPTX
Unit Testng with PHP Unit - A Step by Step Training
PPTX
PHPUnit: from zero to hero
PPT
Phpunit testing
ODT
Testing in-python-and-pytest-framework
PPT
Phpunit
ODP
Python unit testing
PDF
Python testing using mock and pytest
PDF
Test your code like a pro - PHPUnit in practice
PDF
Python unittest
PDF
Python-nose: A unittest-based testing framework for Python that makes writing...
ODP
Pyunit
PPTX
Rc2010 tdd
PDF
Python Testing Fundamentals
 
PPT
20111018 boost and gtest
PPT
Automated hardware testing using python
PPTX
Python Programming Essentials - M31 - PEP 8

More Related Content

PDF
Test Driven Development With Python
 
PPTX
Python Programming Essentials - M35 - Iterators & Generators
PPT
Python testing
PPTX
Python Programming Essentials - M27 - Logging module
PPTX
Python Programming Essentials - M21 - Exception Handling
PPTX
unittest in 5 minutes
PPTX
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
PDF
Python Unit Test
Test Driven Development With Python
 
Python Programming Essentials - M35 - Iterators & Generators
Python testing
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M21 - Exception Handling
unittest in 5 minutes
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Unit Test

What's hot

PPT
Test Driven Development with PHPUnit
PDF
Effective testing with pytest
PDF
Introduction to Unit Testing with PHPUnit
PDF
Modern Python Testing
PPT
Advanced PHPUnit Testing
PPTX
Unit Testng with PHP Unit - A Step by Step Training
PPTX
PHPUnit: from zero to hero
PPT
Phpunit testing
ODT
Testing in-python-and-pytest-framework
PPT
Phpunit
ODP
Python unit testing
PDF
Python testing using mock and pytest
PDF
Test your code like a pro - PHPUnit in practice
PDF
Python unittest
PDF
Python-nose: A unittest-based testing framework for Python that makes writing...
ODP
Pyunit
PPTX
Rc2010 tdd
PDF
Python Testing Fundamentals
 
PPT
20111018 boost and gtest
Test Driven Development with PHPUnit
Effective testing with pytest
Introduction to Unit Testing with PHPUnit
Modern Python Testing
Advanced PHPUnit Testing
Unit Testng with PHP Unit - A Step by Step Training
PHPUnit: from zero to hero
Phpunit testing
Testing in-python-and-pytest-framework
Phpunit
Python unit testing
Python testing using mock and pytest
Test your code like a pro - PHPUnit in practice
Python unittest
Python-nose: A unittest-based testing framework for Python that makes writing...
Pyunit
Rc2010 tdd
Python Testing Fundamentals
 
20111018 boost and gtest

Viewers also liked

PPT
Automated hardware testing using python
PPTX
Python Programming Essentials - M31 - PEP 8
PPTX
Python Programming Essentials - M29 - Python Interpreter and Files
PPTX
Python Programming Essentials - M25 - os and sys modules
PDF
Why I Love Python V2
 
PPTX
Python Programming Essentials - M5 - Variables
PPTX
Python Programming Essentials - M40 - Invoking External Programs
PPTX
Python Programming Essentials - M2 - Introduction to Python
PPTX
Python Programming Essentials - M44 - Overview of Web Development
PPTX
Python Programming Essentials - M28 - Debugging with pdb
PPTX
Python Programming Essentials - M34 - List Comprehensions
ODP
Introduction To Django
PPTX
Python Programming Essentials - M15 - References
ODP
Learn python
Automated hardware testing using python
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M25 - os and sys modules
Why I Love Python V2
 
Python Programming Essentials - M5 - Variables
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M2 - Introduction to Python
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M34 - List Comprehensions
Introduction To Django
Python Programming Essentials - M15 - References
Learn python

Similar to Python Programming Essentials - M39 - Unit Testing

PDF
MT_01_unittest_python.pdf
PPTX
Python: Object-Oriented Testing (Unit Testing)
PDF
PresentationqwertyuiopasdfghUnittest.pdf
PDF
Python and test
PPTX
Coursbjjhuihiuyiyiyuyuiyiuyoilidnes.pptx
PDF
Unit Testing in Python
PPTX
2.Python_Testing_Using_PyUnit_PyTest.pptx
PDF
Py.test
 
PPTX
unittestinginpythonfor-PYDevelopers.pptx
PPTX
2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx
PPTX
1.Python_Testing_Using_PyUnit_Pytest.pptx
PPTX
H testing and debugging
PPTX
Introduction to unit testing in python
PDF
Testing Django Applications
PPTX
Upstate CSCI 540 Unit testing
PDF
New and improved: Coming changes to the unittest module
PDF
Writing tests
PDF
Write unit test from scratch
PPT
unit testing pppttttttttttttttttttttttttttttttttttttttttttttttt
PDF
Debugging 2013- Thomas Ammitzboell-Bach
MT_01_unittest_python.pdf
Python: Object-Oriented Testing (Unit Testing)
PresentationqwertyuiopasdfghUnittest.pdf
Python and test
Coursbjjhuihiuyiyiyuyuiyiuyoilidnes.pptx
Unit Testing in Python
2.Python_Testing_Using_PyUnit_PyTest.pptx
Py.test
 
unittestinginpythonfor-PYDevelopers.pptx
2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx
1.Python_Testing_Using_PyUnit_Pytest.pptx
H testing and debugging
Introduction to unit testing in python
Testing Django Applications
Upstate CSCI 540 Unit testing
New and improved: Coming changes to the unittest module
Writing tests
Write unit test from scratch
unit testing pppttttttttttttttttttttttttttttttttttttttttttttttt
Debugging 2013- Thomas Ammitzboell-Bach

More from P3 InfoTech Solutions Pvt. Ltd.

PPTX
Python Programming Essentials - M6 - Code Blocks and Indentation
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PPTX
Python Programming Essentials - M18 - Modules and Packages
PPTX
Python Programming Essentials - M11 - Comparison and Logical Operators
PPTX
Python Programming Essentials - M23 - datetime module
PPTX
Python Programming Essentials - M20 - Classes and Objects
PPTX
Python Programming Essentials - M24 - math module
PPTX
Python Programming Essentials - M7 - Strings
PPTX
Python Programming Essentials - M13 - Tuples
PPTX
Python Programming Essentials - M8 - String Methods
PPTX
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
PPTX
Python Programming Essentials - M9 - String Formatting
PPTX
Python Programming Essentials - M22 - File Operations
PPTX
Python Programming Essentials - M17 - Functions
PPTX
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
PPTX
Python Programming Essentials - M14 - Dictionaries
PPTX
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M24 - math module
Python Programming Essentials - M7 - Strings
Python Programming Essentials - M13 - Tuples
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M12 - Lists

Python Programming Essentials - M39 - Unit Testing

  • 1.
    http://www.skillbrew.com/SkillbrewTalent brewed bytheindustry itselfUnit TestingPavan Verma@YinYangPavanFounder, P3 InfoTech Solutions Pvt. Ltd.Python Programming Essentials
  • 2.
    © SkillBrew http://skillbrew.comWhatis Unit Testing A software testing method by which individualunits of source code are tested to determine ifthey are fit for use. Unit testing is a software developmentprocess in which the smallest testable parts ofan application, called units, are individuallyand independently scrutinized for properoperation. Unit testing is often automated butit can also be done manually.
  • 3.
    © SkillBrew http://skillbrew.comUnitTesting Glossary test fixture – A test fixture represents thepreparation needed to perform one or moretests, and any associate cleanup actions. Thismay involve, for example, creating temporary orproxy databases, directories, or starting a serverprocess.
  • 4.
    © SkillBrew http://skillbrew.comUnitTesting Glossary (2) test case – A test case is the smallest unitof testing. It checks for a specific responseto a particular set of inputs. Python unittest module provides a baseclass, TestCase, which may be used tocreate new test cases.
  • 5.
    © SkillBrew http://skillbrew.comUnitTesting Glossary (3) test suite – A test suite is a collection oftest cases, test suites, or both. It is used toaggregate tests that should be executedtogether.
  • 6.
    © SkillBrew http://skillbrew.comUnitTesting Glossary (4) test runner – A test runner is a componentwhich orchestrates the execution of testsand provides the outcome to the user. The runner may use a graphical interface,a textual interface, or return a special valueto indicate the results of executing thetests.
  • 7.
    © SkillBrew http://skillbrew.comUnittestmodule The unittest module started life as thethird-party module PyUnit. PyUnit was a Python port of JUnit, theJava unit testing framework.
  • 8.
    © SkillBrew http://skillbrew.comProgramto test – primes.pydef is_prime(number):"""Return True if *number* is prime.""“for element in range(number):if number % element == 0:return Falsereturn True
  • 9.
    © SkillBrew http://skillbrew.comBasicunit test exampleimport unittestfrom primes import is_primeclass PrimesTestCase(unittest.TestCase):"""Tests for `primes.py`.""“def test_is_five_prime(self):"""Is five successfully determined to beprime?""“self.assertTrue(is_prime(5))if __name__ == '__main__':unittest.main()
  • 10.
    © SkillBrew http://skillbrew.comRunninga unit test$ python test_primes.pyE ======================================================================ERROR: test_is_five_prime (__main__.PrimesTestCase)----------------------------------------------------------------------Traceback (most recent call last):File "test_primes.py", line 8, in test_is_five_primeself.assertTrue(is_prime(5))File "/home/jknupp/code/github_code/blug_private/primes.py", line 4, inis_primeif number % element == 0: ZeroDivisionError: integer division or modulo by zero----------------------------------------------------------------------Ran 1 test in 0.000s
  • 11.
    © SkillBrew http://skillbrew.comAssertionsMethodChecks thatassertEqual(a, b) a == bassertNotEqual(a, b) a != bassertTrue(x) bool(x) is TrueassertFalse(x) bool(x) is FalseassertIs(a, b) a is bassertIsNot(a, b) a is not bassertIsNone(x) x is NoneassertIsNotNone(x) x is not NoneassertIn(a, b) a in bassertNotIn(a, b) a not in bassertIsInstance(a, b) isinstance(a, b)assertNotIsInstance(a, b) not isinstance(a, b)
  • 12.
    © SkillBrew http://skillbrew.comAssertions(2)Method Checks thatassertAlmostEqual(a, b) round(a-b, 7) == 0assertNotAlmostEqual(a, b) round(a-b, 7) != 0assertGreater(a, b) a > bassertGreaterEqual(a, b) a >= bassertLess(a, b) a < bassertLessEqual(a, b) a <= bassertRegexpMatches(s, r) r.search(s)assertNotRegexpMatches(s, r) not r.search(s)assertItemsEqual(a, b)sorted(a) == sorted(b) and works withunhashable objsassertDictContainsSubset(a, b) all the key/value pairs in a exist in b
  • 13.
    © SkillBrew http://skillbrew.comsetUpand tearDown#!/usr/bin/pythonimport unittestclass FooTest(unittest.TestCase):"""Sample test case"""# preparing to testdef setUp(self):""" Setting up for the test """print "FooTest:setUp_:begin"## do something...print "FooTest:setUp_:end"# ending the test
  • 14.
    © SkillBrew http://skillbrew.comsetUpand tearDown (2)def tearDown(self):"""Cleaning up after the test"""print "FooTest:tearDown_:begin" ## dosomething...print "FooTest:tearDown_:end" # test routine Adef testA(self):"""Test routine A"""print "FooTest:testA"# test routine Bdef testB(self):"""Test routine B"""print "FooTest:testB"
  • 15.
  • 16.
    © SkillBrew http://skillbrew.comUnittestfeatures setupClass() / tearDownClass() Skip tests Expected failures
  • 17.
    © SkillBrew http://skillbrew.comTestingFrameworks pytest Nose
  • 18.
    © SkillBrew http://skillbrew.comCodecoverage coveragehttp://nedbatchelder.com/code/coverage/
  • 19.
    © SkillBrew http://skillbrew.comReferenceshttps://docs.python.org/2/library/unittest.html http://pytest.org/ http://nose.readthedocs.org/ http://nedbatchelder.com/code/coverage/

[8]ページ先頭

©2009-2025 Movatter.jp