2b8c Tips for writing a test program (Ruby / Python / Java)
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Tips for writing a test program (Ruby / Python / Java)

Where does my test code go?

If you've written a class - a series of methods to be used within another application - how do you test it? How about writing a test program within the same file which runs as the main program if you run your class on its own from the command line, but is ignored if you import / require your class from a higher level application.

In Ruby write your test code within the following condition:

if __FILE__ == $0

In Python, the condition should be written as follows:

if __name__ == "__main__":

And in Java, you can provide a main method in the class.

public static void main (String [] args) {

What goes in my test code?

I suggest that you create at least TWO instances of an object of the type that the class(es) in the file implement, putting fixed values for demonstration purposed in the file if you can so that the test isn't reliant on external data.

Put unique values into each object to that they can clearly seen to be different, and avoid "1"s which won't cause any change if you multiply by them, also avoid 0s, and giving sequences like 1,2,3,4,5 which won't flag up any errors if you use an index number rather than a value by mistake anywhere.

Having created two objects, use a service of methods to set and get attributes, building in tests of the functionality that your users need, and allowing you the chance to see what the interface to your code's going to look like from a user's viewpoint - in fact it's a good idea to write the tst code early.

Some languages have assert statments or whole test harness structures to let you write code which validates its own responses; use them as appropriate. In other cases, print out the results and also say what the results should be so that the tester / user KNOWS whether it has worked, and obviously, from running it.

What does the test code provide

A piece of code for YOU to test your class is functioning and complete when you have written it

A piece of code for you to test that you class is still functioning when you have modified it

A piece of code that lets your users check that your code is working AND that all dependencies have been met when they install or update it

A piece of code that helps you specify the API to your class

A piece of code that illustrates to your users how they may / should call the API that you have provided ... if you do it well / comment it well, it can form a basis of your documentation.

Examples

There is an example of a test harness in Ruby [here] (and that example, which shows you how the main OO features are implemented in Ruby is more fully dosumented [here].

There's a good Python example [here].
(written 2010-01-29, updated 2010-01-30) 2863

 
Associated topics are indexed under
R108 - Ruby - More Classes and Objects
  [3782] Standard methods available on all objects in Ruby - (2012-06-23)
  [3781] Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby? - (2012-06-23)
  [3760] Why you should use objects even for short data manipulation programs in Ruby - (2012-06-10)
  [3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
  [3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
  [3154] Changing a class later on - Ruby - (2011-02-02)
  [3142] Private and Public - and things between - (2011-01-22)
  [2980] Ruby - examples of regular expressions, inheritance and polymorphism - (2010-10-02)
  [2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2623] Object Oriented Ruby - new examples - (2010-02-03)
  [2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
  [2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
  [2603] Ruby objects - a primer - (2010-01-29)
  [2601] Ruby - is_a? v instance_of? - what is the difference? - (2010-01-27)
  [2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
  [1587] Some Ruby programming examples from our course - (2008-03-21)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [184] MTBF of coffee machines - (2005-01-20)

R116 - Ruby - Security Issues.
  [945] Code quality counts - (2006-11-26)

Y106 - Object Oriented Python
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
  [3878] From Structured to Object Oriented Programming. - (2012-10-02)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
  [1306] Python class rattling around - (2007-08-16)
  [900] Python - function v method - (2006-10-20)
  [834] Python makes University Challenge - (2006-08-15)
  [477] Class, static and unbound variables - (2005-10-25)

Y116 - Python - Applying OO design techniques and best practise
  [3887] Inheritance, Composition and Associated objects - when to use which - Python example - (2012-10-10)
  [2523] Plan your application before you start - (2009-12-02)
  [2485] How do I set up a constant in Python? - (2009-10-31)
  [2407] Testing code in Python - doctest, unittest and others - (2009-09-16)
  [2363] Alpaca Case or Camel Case - (2009-08-16)
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)
  [836] Build on what you already have with OO - (2006-08-17)
  [668] Python - block insets help with documentation - (2006-04-04)
  [340] Code and code maintainance efficiency - (2005-06-08)

J608 - Java - Naming Conventions and Code Management
  [2415] Variable names like i and j - why? - (2009-09-22)

J710 - Java - Extending Classes and More
  [3047] What is a universal superclass? Java / Perl / Python / Other OO languages - (2010-11-13)
  [2860] What methods are available on this Java object? - (2010-07-08)
  [2434] Abstract classes, Interfaces, PHP and Java - (2009-10-03)
  [2185] Abstract Classes - Java - (2009-05-16)
  [1819] Calling base class constructors - (2008-10-03)
  [1556] Java - a demonstration of inheritance on just one page - (2008-02-26)
  [1538] Teaching Object Oriented Java with Students and Ice Cream - (2008-02-12)
  [1501] Java - using super to call a method in the parent class - (2008-01-10)
  [1294] An example of Java Inheritance from scratch - (2007-08-00)
  [1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)


5562
Back to
Ruby objects - a primer
Previous and next
or
Horse's mouth home
Forward to
Ruby on Rails - a sample application to teach you how
Some other Articles
Search and replace in Ruby - Ruby Regular Expressions
Answers on Ruby on Rails
Sorting arrays and hashes in Ruby
Ruby on Rails - a sample application to teach you how
Tips for writing a test program (Ruby / Python / Java)
East of Melksham - Building Work Starts
Go Programming Language and Courses?
Telling Apache web servers apart / notes for the non-technical
4088 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2604_Tip ... Java-.html • PAGE BUILT: Sat Feb 23 12:39:13 2013 • BUILD SYSTEM: wizard
0