You can use the unittest module from Python to write unit testing for Websphere’s wsadmin Jython scripts. As of WASv61, this module comes shipped with the product so there is no need to refer externally to your CPython library.
To illustrate, let’s say you want to create a unittest for the Cell.py module below:
class Cell: def __init__(self, AdminControl=None): self.AdminControl = AdminControl def getName(self): return self.AdminControl.getCell()
Here’s a simple class that implements a method to retrieve the name of your Websphere cell.
To write a test case class, you simply create a class that extends unittest.TestCase. Below is an example.
import unittest from Cell import Cell class CellTest(unittest.TestCase): def setUp(self): self.cell = Cell(AdminControl=AdminControl) def tearDown(self): pass def testGetName1(self): self.assertNotEqual(None, self.cell.getName()) def testGetName2(self): id = AdminConfig.list('Cell') name = AdminConfig.showAttribute(id, 'name') self.assertEqual(name, self.cell.getName()) def testGetName3(self): c = Cell() self.assertRaises(AttributeError, c.getName) if __name__ == '__main__' or __name__ == 'main': #unittest.main() # not supported by wsadmin jython # alternative way of running this test suite = unittest.TestLoader().loadTestsFromTestCase(CellTest) unittest.TextTestRunner().run(suite) # other ways of running this test #suite = unittest.TestSuite() #suite.addTest(unittest.makeSuite(CellTest)) #unittest.TextTestRunner().run(suite)
You will notice that I have commented out the line calling unittest.main(). This method call (as of WASv61) does not work with WebSphere’s wsadmin Jython. Fortunately, there are other ways to run a test case, as shown in the code above.
Also, you will notice that there is this odd __name__ == ‘main’ condition. In wsadmin Jython, this is what is being used and not ‘__main__’. For some reason, IBM chose not to use the standard.
Even though Websphere doesn’t support ‘__main__’, I still make it a habit of including it in my test cases for portability reasons. I wanted to have the flexibility of running my test cases using the standard CPython or Jython interpreter, if I need to, like using mock AdminConfig or AdminControl objects. This is also the reason why I chose to inject the AdminControl object into the Cell class instead of importing the module.  This easily allows the use of mock Admin* objects.
 Importing Websphere Admin* objects is not available by default. You have to set it up. See my other post on how to accomplish this – Websphere administrative objects not accessible from imported Jython modules.