Presentation is loading. Please wait.

Presentation is loading. Please wait.

Composition One object is composed of other objects (e.g. Car is composed of an engine, etc.) Engine cylinders : int volume: int start() : None Car engine.

Similar presentations


Presentation on theme: "Composition One object is composed of other objects (e.g. Car is composed of an engine, etc.) Engine cylinders : int volume: int start() : None Car engine."— Presentation transcript:

1 Composition One object is composed of other objects (e.g. Car is composed of an engine, etc.) Engine cylinders : int volume: int start() : None Car engine : Engine tires : int

2 Inheritance Models “type-of” relationships between classes Promotes code re-use Animal formalName : str poop() : None Dog name : str bark() : None Cat name : str scratch() : None Cow moo() : None Base class Superclass Parent class Derived classes Subclasses Child classes

3 Overriding Method in base/super/parent class is overridden by derived/sub/child class Animal formalName : str poop() : None Dog name : str bark() : None poop() : None Cat name : str scratch() : None Cow moo() : None Base class Superclass Parent class Derived classes Subclasses Child classes

4 Testing your code Confidence in your code is proportional to the testing the code has been subjected to. Levels of testing Unit Test the smallest testable unit e.g. a function Integration Test interaction between units e.g. between two functions System Test the complete system Satisfies requirements? Acceptance Test if system is acceptable to business/end user?

5 Unit Testing To validate that each unit of a program works right In OOP, a unit is a method (i.e. function in a class) AAA Unit Testing: Arrange all necessary preconditions and inputs Act on the object or method under test Assert that the expected results have occurred Performed manually or a testing framework Manual AAA example:

6 Unit testing – Manual vs Testing Framework ManualTesting framework Bonus: Time taken by tests

7 Unit testing – Running tests Manual Explicitly call test_* Testing framework Test runner automatically calls test*

8 Unit testing – Per-test setup/teardown Manual Explicitly call functions/set variables before each test Testing framework Define setup/teardown functions once and use decorator to get them to run before/after tests Test runner automatically calls runs setup/teardown functions The setup/teardown functions can be defined at the class-level as well (i.e. before tests begin and after all tests finish)

9 Manual Unit Testing Exception handling Code will stop executing at first exception

10 Framework Unit Testing Exception handling Code will not stop executing at exceptions

11 Unit testing frameworks nose nose (more of a test runner than test framework) Not installed with Python (separate download/install from PyPI) Installs nosetests.exe test runner into \Scripts Has its own classes and methods in nose module to simplify testing (when compared to unittest) nosetests.exe test runner can run unittest & doctest test cases unittest Standard Python module that contains core test framework classes pytest Uses pytest module and assert (built-in function) statements doctest Uses doctest module and >>> comments function docstrings as tests to run and line(s) immediately following as expected results

12 Installing nose from PyPI needs PIP 1.Goto https://pip.pypa.io/en/latest/installing.htmlhttps://pip.pypa.io/en/latest/installing.html 2.Save get-pip.py locally 3.Open a Command Prompt as Administrator 4.CD to path containing get-pip.py 5.Run get-pip.py in Command prompt Use PIP (Python-Installs-Python) to install packages from PyPI Installing PIP

13 Installing nose from PyPI using PIP Open command prompt as Administrator Install nose Installs nose site package into \Lib\site-packages\nose Installs nosetests.exe and related files into \Scripts

14 Inheritance and Python unittest StringUtilsStringUtilsTest unittest.TestCase

15 Unit Testing with unittest Class under test Test class The unittest module contains classes and methods to facilitate testing. Run StringUtilsTest.py

16 Some more unittest details Special methods in the test class setUp() – run before each test method tearDown() – run after each test method Pass:

17 A note about params/args Don’t know the number of arguments a function will require … use * and **


Download ppt "Composition One object is composed of other objects (e.g. Car is composed of an engine, etc.) Engine cylinders : int volume: int start() : None Car engine."

Similar presentations


Ads by Google