I’ve been writing some unit tests for pymqi and fell at the first hurdle. How do I provide logon information? This cannot be hard coded because every one’s system is different. You cannot pass command line parameters to the unittest module, so how do you do it?
The second hurdle was how do I provide configuration information on what to test.
I solved these in two slightly different ways.
Logging on, or connecting to MQ
I created a separate python script called myConnect.py and put the logon information in that.
To be able to use the unit tests, you just have to customise the myConnect.py and use that.
My file does the connection, and had
import sys import platform import pymqi import threading def connect(): queue_manager = 'CSQ9' qmgr = pymqi.connect(queue_manager) return qmgr
In my unit test script I had
import myConnect class inq(unittest.TestCase): def setUp(self): self.qmgr = myConnect.connect()
Simple… and if I need to change my logon information, I just change one file instead of changing all of the scripts.
Passing parameters to the test.
I wanted to be able to pass a “debug option” to the tests, to be able to print information, and to pass other configuration. With the command
python3 -m unittest -v ut.inq1.test_inq_log
you cannot pass parameters.
You can pass environment data for example, passing the variables inline
userid="user1" password="secret1" python -m unittest test
and your test.py has
user = os.environ["userid"] password = os.environ["password"]
External configuration file
I found it easier to set up a utparms.py and store my variables in that, for example
queues = ["CP0000","CP0001"]
I can then import and use the file.
import utparms ... queues = utparms.queues for q in queues: ....
This is different to the myConnect.py above, as the myConnect.py has logic to do the connection. The utparms.py is just a list of variable defines, with no logic.
To use this I can have a shell script like
# recreate the utmarms.py every time cp simpleutparms.py utparms.py python3 -m unittest -v ut.inq1.test_inq_log
cp reallybigutparms.py utparms.py python3 -m unittest -v ut.inq1.test_inq_log
or dynamically create the utparms.py file.
Do not bang your head against a wall when you have a problem – go round it!
You may think these are obvious, but things are only obvious1 when you understand them. Too many people write tests where the configuration information is within the test, and not isolated, so it is clearly not obvious.
1 I remember reading about a university and the definition of obvious:
- If professor X says it is obvious, then it is,
- If professor Y says it is obvious, then after an hours thought it should be obvious,
- If professor Z says it is obvious, then after a year’s work you will understand it.