Module Testing with Python

I really like the Python scripting language a lot. The most basic form of test automation is automated module testing. Whenever I am looking at a programming language with regards to its suitability for software testing, the first thing I do is investigate how well the language can do module testing. There are several versions of Python. CPython is most common on machines with Unix-like operating systems. IronPython is the most common on Windows machines. Check out the image below. I used IronPython to perform interactive, ad hoc module testing of a custom .NET library named TwoCardPokerLib.dll I made for demonstration purposes. IronPython is available from CodePlex, the Microsoft-sponsored open source Web site. You don’t really install IronPython, you simply download a single zipped file, and extract its contents to any convenient directory. I use C:\IronPython. First I launch the IronPython interpreter (ipy.exe) and get the ">>>" Python prompt:
C:\IronPython>ipy.exe -X:TabCompletion
IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.1434
Copyright (c) Microsoft Corporation. All rights reserved.
Next I import the built-in sys module which has all sorts of system-related methods. I view the current Python (as opposed to the Windows system) path:
>>> import sys
>>> sys.path
[‘C:\\IronPython’, ‘C:\\IronPython\\Lib’]
Then I add the location of my module under test so IronPython can find it:
>>> sys.path.append(r’C:\ModuleTestingWithPython\TwoCardPokerLib\bin\Debug’)
After verifying my system path was updated correctly, next I import a module named clr, which is short for Common Langague Runtime. This module allows me to load a custom .NET module using the AddReferenceToFile method:
>>> import clr
>>> clr.AddReferenceToFile("TwoCardPokerLib.dll")
After I import my module, I instantiate two Card objects (representing an Ace of spades and a Nine of hearts):
>>> import TwoCardPokerLib
>>> c1 = TwoCardPokerLib.Card("As")
>>> c2 = TwoCardPokerLib.Card("9c")
Now I call a static Card.Beats() method to test its functionality:
>>> print TwoCardPokerLib.Card.Beats(c1,c2)
My mini-test passes because an Ace beats a Nine. I’m a big fan of Python for lightweight test automation. Check it out sometime!


This entry was posted in Software Test Automation. Bookmark the permalink.