Well House Consultants
You are on the site of
Well House Consultants
Open Source Training Courses
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Well House Manor Resources
Object Orientation and General topics
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
C and C++ Resources
Web and Intranet Resources
Python module Y212
Code testing, patterns, profiles and optimisation.
Exercises, examples and other material relating to training module Y212. This topic is presented on public courses Learning to program in Python
, Python Programming
Robust, reusable and maintainable code is critical to any software project, and at the
heart of achieving these goals are validation and testing systems, standardised and shared
code, and the use of design patterns, utility methods, common libraries, and code coverage checkers.
|Articles and tips on this subject||updated|
|4716||Profiling your Python program|
Python's profiler lets you run code and log statistics, and the pstats module allows you to anaylse the resulting statistics. New example from last week's Intermediate Python Course is [here].
|4618||Pytest - second example beyond hello world|
Moving on from my hello Pytest World example, lets add some more tests and put the code we're testing into a separate file. The test code is [here] and - if working in a TDD (Test Driven Developent) mode it will be written first - forming the specificiation for the class you're writing.
I have brought ...
|4617||Pytest - starting example|
"pytest is a mature full-featured Python testing tool that helps you write better programs" ... and being fully featured, there's something of a hurdle for newcomers in finding which falities they need from the wide range. To help a newcomer today, we took a short look at some first Pytest examples ...
|4542||The principle of mocking - and the Python Mock package|
When I'm writing code, or updating existing code, I need to test it before I release it as generally available.
There are lots of tests I might want to perform, to ensure that all conditions are checked, and that all past bugs have been fixed and have not re-appeared. And indeed as I develop the application, ...
|4540||Unittest of a Flask application including forms|
Moving on from the single file "Hello Flask Test World" at [here], I've now uploaded an example that takes a Flask application including a form - it's [here] and based on our earlier form demo and runs it through unit test [here].
It runs with all tests passing:
WomanWithCat:flask grahamellis$ ...
|4538||Flask and unittest - hello web app test world|
Flask comes with a test client that you can use within unittest - I've put together a really short test of "hello world" [here]. The test example is ugly with the Flask application within the test harness, and should be improved by the importing of the appication to be tested. ...
|4470||Testing in Python 3 - unittest, doctest and __name__ == __main__ too.|
Python's doctest modules takes an interactive session pasted into your documentations string, analyses it and reruns the Python code you used in your session ... telling you whether you're still getting the same results or not.
Firstly, it's a great way of copying tests you've done and recording the ...
|4446||Combining tests into suites, and suites into bigger suites - Python and unittest|
Why are you writing code? To meet a customer requirement! And that code needs to work, to be reliable, and to continue to be reliable into the future as you update it / fix bugs. If you're a programmer, have you ever applied a "quick fix" to something just to discover (perhaps later) that you've broken ...
|4344||Python base and inherited classes, test harness and unit testing - new examples|
From a recent Python course ... a new series of examples on writing, testing and using classes. The customer is using Python 2.6 and 2.7, so the code is written with what we call "new style classes" where everything eventually inherits from a base class os object - with that eventual inheritance expected ...
|4326||Learning to program - comments, documentation and test code|
Updates material from our courses for newcomers to programming ... we're very happy to help newcomers learn about the basic principles of programming, running an extra day for a very small group on the front of our regular courses for delegates who have programmed before, but in different languages.
|4090||Test Driven Development in Python - Customer Comes First|
The Customer comes first!
When you're writing code, you should be thinking of your user. That may be your end user or - if you're writing a module or classes - that might be a fellow developer, set of developers or even yourself. Whichever it is, the important thing is to get it right for them. It ...
|3658||Using Make for a distribution|
Although Make has traditionally been used to build C and C++ programs, its uses are far wider - here's a new example in which I've used it to test a whole series of Python programs, and if they all test correctly to build a distribution (tar) file.
Scenario - three Python programs / classes for distribution. ...
|3478||Testing your Python classes with the unittest package - how to|
Python's unittest module - supplied within the base distribution - allows you to define and run a series of tests for a class or group of classes, and correlate the results, reporting any transgressions in a standard format without having the whole test sequence break.
Testing your code and obtaining ...
|1140||Python GTK - Widget, Packing, Event and Feedback example|
Python has a variety of GUIs available ... one of which, GTK, has been getting much more popular of late. As is common with many of the GUIs in Pyton and other languages, you'll typically write an application as follows:
Initial code, definingWidgetsLayout (Geometry)EventsA main loop, which collects ...
|3464||Passing optional and named parameters to python methods|
When you call a named block of code (a method or function), you'll wish to pass in originating values that are changed from one call / use to the next - these are commonly known as the parameters. If I'm calling a function to calculate the area of a rectangle, for example, I would pass in the width ...
|3442||A demonstration of how many Python facilities work together|
Many of our demonstrations on the Well House Consultants site show individual features of a language - taken in isolaion to show you how they work. However, during a course we usually write further examples to show you how features work in combination to give a total result / solution to an application.
|3441||Pressing ^C in a Python program. Also Progress Bar.|
If you want to stop a user interrupt via ^C aborting your program, you can catch a KeyboardInterrupt in Python. There's an example [here] which I wrote earlier today. It also shows how important it might be to use two try/except blocks rather than one - I've specified two inputs in the same block and, ...
|2616||Defining a static method - Java, Python and Ruby|
Most methods in classes that your write will be run on / applied to individual objects within that class - you'll be asking for the colour of a marker pen, or setting the price of a hotel room. You will NOT - typically - have a model in which all marker pens share the same colour.
But - just occasionally ...
|2123||Using Python with OpenOffice|
The Python-UNO bridge can be used to call up pieces of Python code from within OpenOffice. The source code of a "hello world" example can be found at http://wiki.services.openoffice.org/wiki/PyUNO_bridge, showing you how to start OpenOffice looking for a connection on a TCP/IP port, and then how to ...
|1555||Advanced Python, Perl, PHP and Tcl training courses / classes|
This item is adapted from an answer that I have just written by email - but the question / conundrum as to what to do with fractured advanced training requirements is a common one, so the answer is worth wider sharing. I believe that our solution provides an innovative way of meeting advanced requirements ...
|1146||__new__ v __init__ - python constructor alternatives?|
The constructor you'll normally override (and call when you create an object) in Python is called __init__, but there is also a method called __new__ available to the class author.
If __new__ is defined on a class, it is called in preference to __init__
With __new__ you return the object you have constructed, ...
|1148||Python decorators - wrapping a method call in extra code|
Would you like to add an extra set of wrappers around a series of function of method calls - perhaps to add a profiler or logger to your application? These are examples of the things you can do with Python's decorators, introduced (with the syntax I'll show you below) in Python 2.4.
Here's a class ...
|235||Preparation for a day's work|
Yes, I'm writing "The Horse's Mouth" very late today. It was one of those days where the early morning before the trainees arrived leaked away like water between my fingers, and sending the PHP examples that yesterday's trainees wrote on to them and getting ready for today's tailored advanced Python ...
Examples from our training material
|Combotest|| Combining Test Suites - unittest|
|Stationtest.py|| Test for stations class|
|Traintest.py|| Tests for myTrain classes|
|annie.py|| Test driven exercise|
|dec01|| decorator - to log function calls|
|dt.py|| doctest - a clean example|
|dta.py|| doctest - a longer example|
|dtf.py|| doctest - an example that triggers a failure report|
|dtxfail|| Doctest / Python 3 - failing example|
|dtxpass|| Doctest / Python 3 - passing example|
|first.py|| Test program for use in build demonstration - first principles|
|hound.py|| Object definitions for profiler desmonstration|
|logtest|| Unit test for mary class|
|makefile|| Makefile - for a python test and build distribution project|
|mary.py|| Web server log access class|
|mathstuff.py|| function for Pytest to test|
|mtbf.py|| MTBF class - overwrides + and *, and associated unittest|
|myTrain.py|| train class to be tested|
|mzz|| Mocking - setting up and calling a mocked method|
|profdemo.py|| using and running the profiler and stats analyser|
|railstats.xyz|| data for profiler demo|
|railway.py|| Test class for profiler|
|second.py|| Test program for use in build demonstration - with doctest|
|setup.cfg|| Sample config file for Pytest|
|staboth|| Static Method in Python (classmethod - all versions)|
|stameth|| Static Method in Python (all versions)|
|stations.py|| station class - for testing demo|
|stawithat|| Static Method in Python (Decorator - 2.4 and later)|
|stawrong|| bound method running on a class FAILS|
|tdd_0|| Test Driven Development - step 1|
|tdd_1|| Test Driven Development - multiple objects|
|tdd_2|| Test Driven Developemnt - using unittst|
|tdd_3|| Test Drive Developement - extending unit testing|
|test_hello.py|| Hello Pytest world|
|test_mathfuncs.py|| pytest - simple example with setup|
|test_mathfuncs2.py|| Pytest with setup fixture|
|test_mathfuncs3.py|| Pytest / class test with setup fixture|
|test_mathfuncs4.py|| Pytest - chaining test no setup repeats|
|test_mathfuncs5.py|| Pytest - multiple setup fixtures|
|testtrain|| Unit test - Python 3 / train class|
|thems.py|| Polymorphism demonstration - used to demonstrate the profiler|
|third.py|| Test program for use in build demonstration - with unittest|
|train.py|| class for testing in doctest |
|transport.py|| Sample class - Test Driven Development|
|travel.py|| inheritance, factory, decorator, comparators, static methods, caching, etc|
|trytravel|| unittest for travel.py classes|
|utd2|| Unit Test (2) for transport classes|
|utdemo|| unittest for Transport group of classes|
|utes.py|| Simple unittest demonstration|
|utes2.py|| unittest nesting demonstration|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
Factories and Comparators.
Unittest and Doctest.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.