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:

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. [1] This easily allows the use of mock Admin* objects.

2 Responses to Unit Testing Wsadmin Jython Scripts

hi,
I am trying to run pyunit script with tdlunittest module that would generate xml file. In python it works fine, but when I tried to run in wsadmin, it will give blank xml file. Since module name is __name__ == __main__, I need to use if __name__ == ‘__main__’ in my script. Is there any alternative to import tdlunittest module and genearte xml that would support wsadmin or changing the module name from __main__ to ‘main’.