Tuesday, March 25, 2008

Unit Tests with Iron Python

{

"Testing is the engineering rigor of software development" - Neal Ford

Got a little tip from fuzzyman on using unittest for Unit Testing with Iron Python. Because I'm a slouch I had an old IronPython 1.1 release and kept getting a BaseException error when my tests failed.  The class has since been implemented and should work with the latest version of IronPython that you can download (I'm using a beta release of 2.0).

Here is the code from my unit tests, of course it's destined to change but I was in the mode of getting it to work with IronPython:

from Twining import *
import exceptions
import random
import unittest
import os

class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.cn = "Data Source=.\\sqlexpress;Initial Catalog=MVCBaby;Integrated Security=SSPI;"

def test_exportcsv(self):
""" Export a CSV style data """
outPath = "c:\\foo.csv"
database(self.cn).table("Photos").copyto.csv(outPath)
self.assert_(os.path.exists(outPath), "File was not created")

def test_exportxmlattribstyle(self):
""" Export xml data attribute style """
outPath = "c:\\xml_as_attrib.xml"
database(self.cn).table("Photos").copyto.xmlFieldsAsAttributes(outPath)
self.assert_(os.path.exists(outPath), "File was not created")

def test_exportxmlelementstyle(self):
""" Export xml data attribute style """
outPath = "c:\\xml_as_elem.xml"
database(self.cn).table("Photos").copyto.xmlFieldsAsAttributes(outPath)
self.assert_(os.path.exists(outPath), "File was not created")

def tearDown(self):
if os.path.exists("c:\\foo.csv"): os.remove("c:\\foo.csv")
if os.path.exists("c:\\xml_as_attrib.xml"): os.remove("c:\\xml_as_attrib.xml")
if os.path.exists("c:\\xml_as_elem.xml"): os.remove("c:\\xml_as_elem.xml")

if __name__ == '__main__':
unittest.main()


You can download this and the latest version of things from the new Twining page


}

3 comments:

Fuzzyman said...

Hmmm... we use unittest with IronPython 1.1.1 and don't have any 'BaseException' problems.

You're better off with IronPython 2 for a host of other reasons though.

sevenseeker said...

Do you have any experience with testing existing .NET code and assemblies with IronPython?

How would I go about reflecting and introspecting elements to see what I can do if I don't have access to all of the code?

Fuzzyman said...

Introspecting .NET objects from IronPython is really easy.

dir(someObject)

returns a list of strings of member names (public and protected) of that object.

help(someObject.someMethod)

can also be useful sometimes.

Lots of other easy reflection techniques (hasattr, getattr and setattr for example).

Experimenting from the interactive interpreter will give you a feel for what is possible.